首先,我认为这个问题不是C#独立的.但也可以用于其他语言,如C.
我现在正在尝试解析以4字节little-endian格式存储整数的文件格式.TBH,我不知道little-endian格式和big-endian格式是如何工作的.
但我需要将它们转换为可用的int变量.
例如,02 00 00 00 = 2
到目前为止,我有这个代码转换前2个字节:(我使用FileStream.Read将原始数据存储到缓冲区变量)
int num = ((buffer[5] << 8) + buffer[4]);
Run Code Online (Sandbox Code Playgroud)
但它只会转换前两个字节.(示例中为02 00,而不是02 00 00 00)
任何形式的帮助将不胜感激:)
:我是从分析数据集MNIST在C#http://yann.lecun.com/exdb/mnist/
我正在尝试Int32从二进制文件中读取第一个:
FileStream fileS = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fileS);
int magicNumber = reader.ReadInt32();
Run Code Online (Sandbox Code Playgroud)
但是,它给了我一个无意义的数字:50855936.
如果我使用 File.ReadAllBytes()
buffer = File.ReadAllBytes(fileName);
Run Code Online (Sandbox Code Playgroud)
然后查看字节,它工作正常(前四个字节现在代表2049),我做了什么错误的BinaryReader?
文件格式如下(我正在尝试读取第一个幻数):
All the integers in the files are stored in the MSB first (high endian) format used by most non-Intel processors. Users of Intel processors and other low-endian machines must flip the bytes of the header.
Run Code Online (Sandbox Code Playgroud)
TRAINING SET LABEL FILE(train-labels-idx1-ubyte):
[offset] [type] [value] [description]
0000 32 bit integer 0x00000801(2049) magic number (MSB …Run Code Online (Sandbox Code Playgroud)