converting bytes to integers in a bytebuffer

0

I have found this piece of code that I have posted below that should convert bytes to integers, however I do not fully understand how this piece of code works in java. I believe bytebuffer is used in this case in a full piece of code as it is just a part I am struggling to understand.

public static byte[] intToBytes(int i) {
    return new byte[] {
        (byte) ((i & 0x000000FF) >> 0),
        (byte) ((i & 0x0000FF00) >> 8),           
        (byte) ((i & 0x00FF0000) >> 16),
        (byte) ((i & 0xFF000000) >> 24)
    };
java
bytebuffer
asked on Stack Overflow Apr 3, 2020 by Silvija • edited Apr 3, 2020 by megamind

1 Answer

0
public static int bytesToInt(byte[] bytes) {
    return ByteBuffer.wrap(bytes).getInt();
}
answered on Stack Overflow Apr 3, 2020 by saka1029

User contributions licensed under CC BY-SA 3.0