如何将整数转换为无符号的32位大字节序字节数组 [英] How do I convert an integer to unsigned 32 bit big endian byte array
问题描述
我有一个整数,代表帧长.我想知道如何在Java中将整数转换为无符号的32位(4字节)大字节序字节数组.
I have an integer which represents a frame length. I would like to know how I can convert the integer to an unsigned 32 bit (4 bytes) big endian byte array in Java
推荐答案
在Java中,整数是带符号的,并且是大端的.单数以二进制补码格式编码.要将二进制补码转换为无符号数,只需遵循以下规则:
In Java, integer is signed and big endian. Singed number are encoded in two's complement format. To convert a two's complement number to unsigned number, just follow the following rules:
如果要使用无符号表示,只需遵循规则即可获取无符号编号.由于整数已经在big endian中,因此只需将结果分成4个字节即可.
If you want a unsigned representation, just following the rules to get the unsigned number. As the integer already in big endian, just split the result in 4 bytes.
这篇关于如何将整数转换为无符号的32位大字节序字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!