BitConverter.ToInt16 将 0xFFFF 添加到 Number? (C#)

发布于 2024-10-01 10:52:41 字数 504 浏览 1 评论 0原文

我在这里遇到一个问题,这可能是我只是忽略的问题,但我无法理解为什么会发生......

我遇到的问题是我正在使用位转换器给我一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-10-08 10:52:41

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.

森罗 2024-10-08 10:52:41

0xffffa388 不是 Int16。您确定不将其转换为某种 32 位类型吗?

0xffffa388 is not an Int16. Are you sure you're not casting it to some 32-bit type?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文