4

I need to get unsigned integer from byte array. I understand that java doesn't support unsigned primitives, and I have to use higher primitive (long) to get unsigned int. Many people usually suggest solution like:

public static long getUnsignedInt(byte[] data)
{
    ByteBuffer bb = ByteBuffer.wrap(data);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getInt() & 0xffffffffl;
}

But this is not smart since we have to get signed integer then convert it to unsigned which of course may result in overflow exception. I saw other solutions using BigInteger or new java 8 unsigned feature, but I couldn't get it to do what I want.

7
  • I couldn't get it to do what I want Can you clarify what you want? Commented Jul 31, 2015 at 15:48
  • 1
    @SotiriosDelimanolis I believe OP is looking for an alternate way (using BigInteger) to generate an unsigned int from a byte[] Commented Jul 31, 2015 at 15:50
  • Take a look at this answer: stackoverflow.com/a/1576404/1011791 Commented Jul 31, 2015 at 15:50
  • 2
    You have 4 bytes. You can't skip to a long directly. You need to get the int first. Commented Jul 31, 2015 at 15:51
  • 2
    Are you sure your input bytes are in little endian byte order? Apart from that, I think the code in the question is better than the one you accepted... Commented Jul 31, 2015 at 18:05

2 Answers 2

3

But this is not smart since we have to get signed integer then convert it to unsigned which of course may result in overflow exception.

There is no such thing as an "overflow exception." Your solution will always work exactly correctly and efficiently. Stop worrying.

Sign up to request clarification or add additional context in comments.

1 Comment

This is obviously the correct answer. However, I'm thinking maybe it should be a comment and the question closed, as it seems entirely based on a misconception.
1

You could do something like this:

public static long getUnsignedInt(byte[] data) {
    long result = 0;

    for (int i = 0; i < data.length; i++) {
        result += data[i] << 8 * (data.length - 1 - i);
    }
    return result;
}

You basically create an empty long and shift the bytes into it. You can see this in action in the java.io.DataInputStream.readInt() method.

4 Comments

this will may get a negative result or wrong result if the last byte is larger than 128.
@Eki Considering that an int in Java is 32 bits long and we return a long (64 bits) this will not happen unless you try to build an int with 8 bytes, which is nonesense.
no, i use a 4 bytes array as input and only last byte has a value larger than 128 and than get a negative output. I think the reason is when you do "result += somebyte", java will convert somebyte into long type implicitly so actually result will plus a negative long value converted by somebyte.
Hm, you are right, with unsigned bytes it wouldn't matter (C, etc.). Try doing a bitwise or.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.