So let's say I've got two unsigned bytes coming from a device to my software:
And I can read their value by:
int first = buffer[0] & 0xFF;
int second = buffer[1] & 0xFF;
And then how can convert these two numbers into an int in Java?
In short: convert unsigned byte buffer array into an int
More details:
So let's say if I have a signed byte array, I can convert it to an int by this way:
int val = ((bytes[0] & 0xff) << 8) | (bytes[1] & 0xff);
but then what should I do to convert an unsigned byte array to an int?
// UPDATED:
Just figured out the way to convert it:
private int toInt(byte[] b) {
int x = (0 << 24) | (0 << 16)
| ((b[1] & 0xFF) << 8) | ((b[0] & 0xFF) << 0);
return x;
}