相关疑难解决方法(0)

在.NET中反转字节顺序

在下面的代码中,为什么X和Y采用的不同于我想象的直观值?

如果将字节0-7写入缓冲区,那么得到的long不应该具有相同顺序的字节吗?这就像它以相反的顺序读取长值.

x    0x0706050403020100    long
y    0x0706050403020100    long
z    0x0001020304050607    long

MemoryStream ms = new MemoryStream();
byte[] buffer = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
ms.Write(buffer, 0, buffer.Length);
ms.Flush();
ms.Position = 0;

BinaryReader reader = new BinaryReader(ms);
long x = reader.ReadInt64();
long y = BitConverter.ToInt64(buffer, 0);
long z = BitConverter.ToInt64(buffer.Reverse<byte>().ToArray<byte>(), 0);

byte[] xbytes = BitConverter.GetBytes(x);
byte[] ybytes = BitConverter.GetBytes(y);
byte[] zbytes = BitConverter.GetBytes(z);
Run Code Online (Sandbox Code Playgroud)

(除了.NET之外,我不知道要标记这个问题的内容.)


BitConverter.IsLittleEndian
Run Code Online (Sandbox Code Playgroud)

是假的.如果我的电脑是大端,为什么会这样?

  • 这是一台Windows 7 64位计算机
  • Intel Core2 Quad Q9400 2.66 …

.net endianness

13
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net ×1

endianness ×1