What did I do wrong with parsing MNIST dataset with BinaryReader in C#?

前端 未结 2 551

I\'m parsing MNIST datasets in C# from: http://yann.lecun.com/exdb/mnist/

I\'m trying to read the first Int32 from a binary file:

File         


        
2条回答
  •  孤街浪徒
    2021-01-13 16:06

    50855936 == 0x03080000. Or 0x00000803 when you reverse the bytes, required on almost any machine since little-endian has won the egg war. Close enough to 2049, no great idea what explains the offset of 2. Here's an extension method to help you read it:

      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);
          }
      }
    

    Add additional methods if the file contains more field types, just substitute Int32 in the snippet.

提交回复
热议问题