converting bytes to int in Java

前端 未结 5 1141
隐瞒了意图╮
隐瞒了意图╮ 2021-01-05 05:24

I need to convert 2 bytes (2\'s complement) into an int in Java code. how do I do it?

toInt(byte hb, byte lb)
{

}
5条回答
  •  生来不讨喜
    2021-01-05 05:58

    return ((int)hb << 8) | ((int)lb & 0xFF);
    

    Correct operation in all cases is left as an exercise for the student.

提交回复
热议问题