How to read two bytes from array of bytes into an int in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        byte[] bytes = new byte[] {(byte)0x10, (byte)0x3F, (byte)0x1D, (byte)0x01, (byte)0x2E};
        
        int n = ((bytes[1] & 0xff) << 8) | (bytes[2] & 0xff);
        
        System.out.println(n);
    }
}



/*
run:

16144

*/

 



answered Mar 17, 2021 by avibootz

Related questions

1 answer 171 views
2 answers 291 views
1 answer 157 views
2 answers 244 views
1 answer 251 views
...