【发布时间】:2014-01-24 20:49:17
【问题描述】:
我正在解析 C# 中的 MNIST 数据集,来自: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();
但是,它给了我一个无意义的数字:50855936。
如果我使用File.ReadAllBytes()
buffer = File.ReadAllBytes(fileName);
然后查看字节,它工作正常(前四个字节现在代表 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.
训练集标签文件(train-labels-idx1-ubyte):
[offset] [type] [value] [description]
0000 32 bit integer 0x00000801(2049) magic number (MSB first)
0004 32 bit integer 60000 number of items
0008 unsignebyte ?? label
0009 unsigned byte ?? label
........
xxxx unsigned byte ?? label
The labels values are 0 to 9.d
【问题讨论】:
-
你的文件是什么编码的?默认情况下,BinaryReader 使用 UTF-8。如果它不同于 UTF-8,请尝试在 BinaryReader 构造函数调用中指定它。
-
你能举个例子说明你觉得哪里不对吗?
-
字节序有问题?
-
它返回什么,你期待什么?可能是字节序的问题。
-
好吧,现在
ReadInt32的文档以及您添加的内容应该清楚了。 “BinaryReader 以 little-endian 格式读取此数据类型。”
标签: c# binary endianness mnist