BitConverter.ToInt16 将 0xFFFF 添加到 Number? (C#)
我在这里遇到一个问题,这可能是我只是忽略的问题,但我无法理解为什么会发生......
我遇到的问题是我正在使用位转换器给我一个 Int16一个 2 字节数组,但由于某种原因,每当我这样做时,我都会得到我应该得到的数字,并将 0xFFFF 添加到数字的开头。
示例...
byte[] ourArray = { 0x88, 0xA3, 0x67, 0x3D };
Int16 CreationDate = BitConverter.ToInt16(new byte[] {ourArray[2], ourArray[3]} , 0);
Int16 CreationTime = BitConverter.ToInt16(new byte[] {ourArray[0], ourArray[1]}, 0);
返回的“CreationDate”为 0x3d67(正确),但 CreationTime 为 0xffffa388。
有人知道为什么会发生这种情况,以及纠正这种情况的方法吗?
I've got a problem here that's probably something that I'm just overlooking, but I can't understand why it's happening...
The problem I'm having is that I'm using the bit converter to give me an Int16 from a 2-byte array, but for some reason whenever I do this -- I get the number I should get, with 0xFFFF added to the beginning of the number.
Example...
byte[] ourArray = { 0x88, 0xA3, 0x67, 0x3D };
Int16 CreationDate = BitConverter.ToInt16(new byte[] {ourArray[2], ourArray[3]} , 0);
Int16 CreationTime = BitConverter.ToInt16(new byte[] {ourArray[0], ourArray[1]}, 0);
That will return with "CreationDate" being 0x3d67 (correct), but CreationTime being 0xffffa388.
Would anyone happen to know why this is happening, and a way to correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
0xA388 是负 Int16,因此转换为 Int32 将给出具有相似值的 符号扩展 负 int。您看到的 0xFFFF 是符号扩展(用“1”位填充)。最好使用 UInt16 和 UInt32。
0xA388 is a negative Int16, so converted to Int32 will give a sign extended negative int with similar value. That 0xFFFF you see is the sign extension (padding with '1' bits). Better use UInt16 and UInt32.
0xffffa388 不是 Int16。您确定不将其转换为某种 32 位类型吗?
0xffffa388 is not an Int16. Are you sure you're not casting it to some 32-bit type?