I have some data in int variables in Java (range between 0 and 64000). How to convert to byte this integer? I need just two lower bytes from int (range is ok). How to extract this? 
You can get the lowest byte from the integer by ANDing with 0xFF:
byte lowByte = (byte)(value & 0xFF);
This works because 0xFF has zero bits everywhere above the first byte.
To get the second-lowest-byte, you can repeat this trick after shifting down all the bits in the number 8 spots:
byte penultimateByte = (byte)((value >> 8) & 0xFF);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With