我从一个带符号的字节数组开始并转换为unsigned ..打印结果是否正确?
byte[] unsigned = new byte[] {10,100,120,180,200,220,240};
sbyte[] signed = Utils.toSignedByteArray(unsigned);
打印(我只是用StringBuilder附加它们):
签署:[10,100,120,-76,-56,-36,-16]
无符号:[10,100,120,180,200,220,240]
其中:
public static sbyte[] toSignedByteArray(byte[] unsigned){
sbyte[] signed = new sbyte[unsigned.Length];
Buffer.BlockCopy(unsigned, 0, signed, 0, unsigned.Length);
return signed;
}
如果我改为此,我会得到相同的结果。
sbyte[] signed = (sbyte[])(Array)unsigned;
不应该-128(签名)变为0,-118变为10,依此类推......而不是10(签名)= 10(无符号)!?
因为
sbyte -128到127
字节0到255
SO -
答案 0 :(得分:6)
有符号整数表示在Two's complement system。
中示例:
Bits Unsigned 2's complement
value value
00000000 0 0
00000001 1 1
00000010 2 2
01111110 126 126
01111111 127 127
10000000 128 −128
10000001 129 −127
10000010 130 −126
11111110 254 −2
11111111 255 −1