[英]Converting Byte Array into short - strange results
I am converting an 2 bytes long byte array into an short (Int16) and showing it via a messagebox. 我将一个2字节长的字节数组转换为短(Int16),并通过消息框显示它。
MessageBox.Show(BitConverter.ToString(arr) + "\n" + BitConverter.ToInt16(arr, 0));
But the results are a bit strange. 但是结果有点奇怪。 Here a few examples: 这里有几个例子:
Array Short Correct Result
-------------------------------
00-60 24576 24576
AB-2A 10923 10923
55-55 21845 21845
00-80 -32768 32768
AB-AA -21845 43691
55-D5 -10923 54613
You're using ToInt16
, which returns numbers from -32768 to 32767, so numbers out of range will be truncated or adjusted to fit the range. 您正在使用ToInt16
,它返回从-32768到32767的数字,因此超出范围的数字将被截断或调整以适合该范围。 For numbers from 0 through 65535, which seems to be what you want, use ToUInt16
instead: 对于0到65535之间的数字(似乎是您想要的),请改用ToUInt16
:
MessageBox.Show(BitConverter.ToString(arr) + "\n" + BitConverter.ToUInt16(arr, 0));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.