How do I split an integer into 2 byte binary?

前端 未结 7 1253
再見小時候
再見小時候 2020-12-12 22:22

Given

private int width = 400;
private byte [] data = new byte [2];  

I want to split the integer \"width\" into two bytes and load data[0]

7条回答
  •  情书的邮戳
    2020-12-12 22:41

    Integer is 32 bits (=4 bytes) in java, you know?

    width & 0xff will give you the first byte, width & 0xff00 >> 8 will give you the second, etc.

提交回复
热议问题