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.