roo*_*ter 4 c# binaryfiles binary-data
我正在尝试使用读取文件BinaryReader.但是,我在检索预期的值时遇到问题.
using (BinaryReader b = new BinaryReader(File.Open("file.dat", FileMode.Open)))
{
int result = b.ReadInt32(); //expected to be 2051
}
Run Code Online (Sandbox Code Playgroud)
"file.dat" 是以下......
00 00 08 03 00 00 EA 60
Run Code Online (Sandbox Code Playgroud)
预期的结果应该是2051,但它得到的东西完全无关紧要.请注意,我每次得到的结果都是一样的.
问题是什么?
BinaryReader.ReadInt32期望数据采用Little Endian格式.您提供的数据是Big Endian.
这是一个示例程序,显示BinaryWriter如何将Int32写入内存的输出:
namespace Endian {
using System;
using System.IO;
static class Program {
static void Main() {
int a = 2051;
using (MemoryStream stream = new MemoryStream()) {
using (BinaryWriter writer = new BinaryWriter(stream)) {
writer.Write(a);
}
foreach (byte b in stream.ToArray()) {
Console.Write("{0:X2} ", b);
}
}
Console.WriteLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行它会产生输出:
03 08 00 00
Run Code Online (Sandbox Code Playgroud)
要在两者之间进行转换,您可以使用读取四个字节BinaryReader.ReadBytes(4),反转数组,然后使用BitConverter.ToInt32它将其转换为可用的int.
byte[] data = reader.ReadBytes(4);
Array.Reverse(data);
int result = BitConverter.ToInt32(data);
Run Code Online (Sandbox Code Playgroud)
00 00 08 03 是 2051,但如果字节实际上在您列出的顺序文件中,则它们的顺序错误.应将四字节整数0x0803存储为03 08 00 00- 最低有效字节优先或"小端".
随便我怀疑你得到50855936作为答案?这是00 00 08 03最重要的字节顺序,"big-endian".
x86架构是小端的; 大多数其他架构都是大端的.您的数据文件可能保存在big-endian机器上,或者显式保存为big-endian,因为这是"Internet"的标准字节顺序.
要从big-endian转换为little-endian,你只需要切换四个字节的顺序.最简单的方法是IPAddress.NetworkToHostOrder方法("网络"顺序是big-endian; x86的"host"顺序是little-endian.)