2 bytes to short java

后端 未结 5 1646
迷失自我
迷失自我 2020-11-29 03:40

i\'m reading 133 length packet from serialport,last 2 bytes contain CRC values,2 bytes value i\'ve make single(short i think) using java. this what i have done,



        
5条回答
  •  自闭症患者
    2020-11-29 04:04

    You can convert 2 bytes to a short in a more readable and elegant way.

    short s = ByteBuffer.wrap(new byte[]{0x01, 0x02}).getShort();
    // now s equals 258 = 256 + 2
    

    The first byte is the most significant byte.

提交回复
热议问题