Results 1 to 3 of 3
  1. #1
    radhika.putty is offline Member
    Join Date
    Mar 2011
    Rep Power

    Default 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

  2. #2
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Blog Entries
    Rep Power

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Rep Power


    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

  1. Converting from long to int
    By peterhabe in forum New To Java
    Replies: 2
    Last Post: 02-20-2011, 07:14 PM
  2. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  3. again -.- unsigned types
    By prich in forum Networking
    Replies: 1
    Last Post: 10-17-2009, 05:53 PM
  4. Sending unsigned bytes using DatagramPacket
    By sranil in forum Networking
    Replies: 2
    Last Post: 04-28-2009, 02:52 AM
  5. stumpt. converting an object to a long int.
    By root_demon in forum New To Java
    Replies: 7
    Last Post: 10-31-2008, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts