Solved

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

Posted on 2014-09-03
8
376 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 28

Assisted Solution

by:pepr
pepr earned 200 total points
Comment Utility
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
Comment Utility
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 200 total points
Comment Utility
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
 
LVL 5

Assisted Solution

by:Jan Louwerens
Jan Louwerens earned 100 total points
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:thready
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks everyone, sorry for the late reply!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
You're welcome.
Thanx 4 axxepting
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now