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

Posted on 2014-09-03
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?

Question by:thready
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
LVL 29

Assisted Solution

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).
LVL 37

Accepted Solution

Neil Russell earned 100 total points
ID: 40302120
LVL 29

Assisted Solution

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.
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.


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


Author Comment

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)
LVL 37

Assisted Solution

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

Author Closing Comment

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

Expert Comment

ID: 40316354
You're welcome.
Thanx 4 axxepting

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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…

691 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