Converting 32-bit unsigned integer (big endian) to long and back

后端 未结 3 2048
北恋
北恋 2020-12-16 03:57

I have a byte[4] which contains a 32-bit unsigned integer (in big endian order) and I need to convert it to long (as int can\'t hold an unsigned number).

Also, how d

3条回答
  •  轮回少年
    2020-12-16 04:25

    You can use ByteBuffer, or you can do it the old-fashioned way:

    long result = 0x00FF & byteData[0];
    result <<= 8;
    result += 0x00FF & byteData[1];
    result <<= 8;
    result += 0x00FF & byteData[2];
    result <<= 8;
    result += 0x00FF & byteData[3];
    

提交回复
热议问题