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
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.