Solved

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

Posted on 2014-09-03
8
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

738 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