How can I convert a 4-byte array to an integer?

后端 未结 7 722
梦如初夏
梦如初夏 2020-12-24 02:42

I want to perform a conversion without resorting to some implementation-dependent trick. Any tips?

7条回答
  •  囚心锁ツ
    2020-12-24 03:15

    public static int toInt( byte[] bytes ) {
    int result = 0;
    for (int i=0; i<3; i++) {
      result = ( result << 8 ) - Byte.MIN_VALUE + (int) bytes[i];
    }
    return result;
    }
    

提交回复
热议问题