Solved

java reading an unsigned int from file (into an int) and showing me the value as negative

Posted on 2014-09-03
8
399 Views
Last Modified: 2014-09-11
Hi Experts,

I'm reading an int from a stream in java and I know what I'm reading is an unsigned int, and obviously it's a big number because I get the negative sign on it (representation is negative because the negative bit is set).

I would like to know what the int value of -1083351007 is as an unsigned int?

Thanks,
Mike
0
Comment
Question by:thready
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 29

Assisted Solution

by:pepr
pepr earned 200 total points
ID: 40302114
I am not good in Java, but as far as I know, there is no unsigned integer there. You need to declare the variable as long (or the class Long wrapper).
0
 
LVL 37

Accepted Solution

by:
Neil Russell earned 100 total points
ID: 40302120
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 200 total points
ID: 40302130
If you know you need to get only four bytes from the stream, you may need to read it as four bytes, detect the endianness, and compose the long value out of those four bytes.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Assisted Solution

by:Jan Louwerens
Jan Louwerens earned 100 total points
ID: 40302136
It look like it's overflowing as an int value. Try readind it as a long value.

To convert that number into it's unsigned equivalent:
      int intValue = -1083351007;
      long longValue = Long.valueOf(intValue & 0xFFFFFFFFl);

Open in new window

0
 
LVL 1

Author Comment

by:thready
ID: 40302150
all good answers, thanks everyone- I'm on the right track.  Although it is overflowing, all the data is in my variable as expected.  I will be back soon with points!  :)

The conversion to 3211616289 made me very happy  :o)
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 100 total points
ID: 40302815
And if you know the insides
(
  - the maximum number that fits in an int is Integer.MAX_VALUE
  - a negative int is in fact a number that is too big to fit in an int
  - a negative int is Integer.MAX_VALUE plus the part that doesn't fit, being the number minus Integer.MAX_VALUE
)
here's yet another way to do it:

long longValue2 = (long)Integer.MAX_VALUE + (long)(intValue - Integer.MAX_VALUE);
System.out.println(longValue2);  // this is printing out 3211616289
0
 
LVL 1

Author Closing Comment

by:thready
ID: 40316107
Thanks everyone, sorry for the late reply!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 40316354
You're welcome.
Thanx 4 axxepting
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question