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

前端 未结 2 544

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:21

    It seems that your problem is somewhere else. Could you post a minimal compilable snippet that doesn't work as expected?

    For example, this snippet works exactly as expected - it creates a binary file of 8 bytes, which are two big-endian Int32s. The reader then correctly reads the data as the two integers.

    using (var str = File.Create("C:\\Test.dat"))
        using (var wr = new BinaryWriter(str))
        {
            wr.Write(2049);
            wr.Write(60000);
        }
    
    using (var str = File.Open("C:\\Test.dat", FileMode.Open))
        using (var rdr = new BinaryReader(str))
        {
            rdr.ReadInt32().Dump();
            rdr.ReadInt32().Dump();
        }
    

    However, the endianness is fixed. If you need to use MSB first, you need to read the bytes and convert them to integers yourself (or, you could of course invert the byte order using bitwise operations, if you're so inclined).

提交回复
热议问题