-
I want to convert bytes to int in Java.
I want to assume bytes to be unsigned bytes.
Suppose ifbyte a = (byte)0xFF; int r = (some operation on byte a);r should be 255 not -1 in decimal.
-
Then I want to create int value from 3 bytes.
Suppose ifbyte b1 = (byte)0x0F; byte b2 = (byte)0xFF; byte b3 = (byte)0xFF; int r = (some operation in bytes b1, b2 and b3);Then r should be
0x000FFFFF. Byte b1 will be placed at higher 3rd position and byte b3 will be placed at lower 1st position in int value. Also my b1 will range from 0x00 to 0x0F and other bytes will be from0x00to0xFF, assuming unsigned nature of bytes. If byte b1 is greater than 0x0F, I will extract only lowermost 4 bits. In short I want to extract int from 3 bytes but using only 20 bits of 3 bytes. (total 16 bits from b2 and b3, and 4 lowermost bits from b1). int r must be positive as we are creating from 3 bytes and assuming unsigned nature of bytes.
I want to convert bytes to int in Java. I want to assume bytes
Share
You have to be careful with the sign-extension here – unfortunately, bytes are signed in Java (as far as I know, this has caused nothing but grief).
So you have to do a bit of masking.