如果我有一个长度为n的byte [],包含某个n字节整数的2的补码表示,我如何得到一个相同长度的byte [],并用“直”表示该数字?这是否可能,考虑到2的补数是否可以表示更多的负整数?
通过“直线”表示我的意思是将整数范围连续映射到某个字节范围,而不会跳跃,打孔等。
e.g。
for the 1-byte integer -128, the two's complement representation is 1000 0000
for the 1-byte integer -1, the two's complement representation is 1111 1111
for the 1-byte integer 0, the two's complement representation is 0000 0000
for the 1-byte integer 1, the two's complement representation is 0000 0001
for the 1-byte integer 127, the two's complement representation is 0111 1111
我正在寻找的是将上述值转换为类似的东西:
for the 1-byte integer -128, the "straight" representation would be 0000 0000
for the 1-byte integer -1, the "straight" representation would be 0111 1111
for the 1-byte integer 0, the "straight" representation would be 1000 0000
for the 1-byte integer 1, the "straight" representation would be 1000 0001
for the 1-byte integer 127, the "straight" representation would be 1111 1111
我在这里所谓的“直接”表示是否有任何正确的名称?
我更喜欢解决方案中的Java,但任何其他容易/直观易懂的语言或伪代码都可以。 (在此转换中是否有任何特殊要警惕,因为Java没有无符号字节?)