?
Solved

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

Posted on 2014-09-03
8
Medium Priority
?
433 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 800 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 400 total points
ID: 40302120
0
 
LVL 29

Assisted Solution

by:pepr
pepr earned 800 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 5

Assisted Solution

by:Jan Louwerens
Jan Louwerens earned 400 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 400 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 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