加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中使用BinaryReader解析MNIST数据集我做错了什么?

发布时间:2020-12-16 01:59:21 所属栏目:百科 来源:网络整理
导读:我在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 =
我在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.

TRAINING SET LABEL FILE(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

解决方法

50855936 == 0x03080000.或者0x00000803,当你反转字节时,几乎所有机器都需要,因为little-endian赢得了蛋战.足够接近2049年,不知道是什么解释了2的偏移.这是一个帮助你阅读它的扩展方法:

public static class BigEndianUtils {
      public static int ReadBigInt32(this BinaryReader br) {
          var bytes = br.ReadBytes(sizeof(Int32));
          if (BitConverter.IsLittleEndian) Array.Reverse(bytes);
          return BitConverter.ToInt32(bytes,0);
      }
  }

如果文件包含更多字段类型,则添加其他方法,只需在代码段中替换Int32即可.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读