Reputation: 1387
I'm using this function to convert bytearray to int.
public static int byteArrayToInt(byte[] b) {
ByteBuffer bb = ByteBuffer.wrap(b);
bb.order(ByteOrder.LITTLE_ENDIAN);
return bb.getInt();
}
Unfortunately I'm getting java.nio.BufferUnderflowException at the return statement. I'm calling this function many times by the way. I read that it happens when more bytes are being read than the buffer size. But can anyone tell me how to avoid it?
Is there a way to copy my original bytearray to new bytearray of size 4 and pass this bytearray to the function?
UPDATE: I tried this in main method:
byte[] bbTemp=new byte[4];
bbTemp = originalbyteArray;
And passed bbTemp to the above function, but it doesn't work. Any idea why?
Upvotes: 0
Views: 4667
Reputation: 280168
A BufferUnderflowException
will be thrown
If there are fewer than four bytes remaining in this buffer
Make sure your byte[]
has at least 4 bytes in it, which is the size of an int
.
Upvotes: 3