Results 1 to 3 of 3
Thread: Converting unsigned long to java
- 03-31-2011, 11:36 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 1
- Rep Power
- 0
Converting unsigned long to java
I am trying to read a file written by C code in binary format. When i try to read data stored as unsigned long using Java i am getting wrong value because of the overflow caused by signed & unsigned probelm . I tried using BigInteger and also tried masking but nothing helped. is there any API available for this. I think we need to manually check the magnitude and do something but did anyone face this issue and found a work around
- 03-31-2011, 07:00 PM #2
Senior Member
- Join Date
- Nov 2010
- Location
- Delhi
- Posts
- 135
- Blog Entries
- 1
- Rep Power
- 0
I think this link biginteger - C /C++ long long to Java long - Stack Overflow might help you.
- 04-01-2011, 05:31 AM #3
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Create a BigInteger to hold the number. When you read it in, if it's negative, you know there was an overflow. In that case, add that negative BigInteger to another BigInteger that is Long.MAX_VALUE to get the unsigned value.
Alternately, depending on what you're doing with the number, you might be able to simply deal with the negative values in a conditional, knowing they are really positive.
As an alternate alternative, you could read the data into 2 java longs, with the upper 4 bytes in read into the lower 4 bytes of long1 and the lower 4 bytes read into the lower 4 bytes of long2.
Similar Threads
-
Converting from long to int
By peterhabe in forum New To JavaReplies: 2Last Post: 02-20-2011, 06:14 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
again -.- unsigned types
By prich in forum NetworkingReplies: 1Last Post: 10-17-2009, 05:53 PM -
Sending unsigned bytes using DatagramPacket
By sranil in forum NetworkingReplies: 2Last Post: 04-28-2009, 02:52 AM -
stumpt. converting an object to a long int.
By root_demon in forum New To JavaReplies: 7Last Post: 10-31-2008, 05:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks