I read a file into a struct using this code:
StructType aStruct;
int count = Marshal.SizeOf(typeof(StructType));
byte[] readBuffer = new byte[count];
BinaryReader reader = new BinaryReader(stream);
readBuffer = reader.ReadBytes(count);
GCHandle handle = GCHandle.Alloc(readBuffer, GCHandleType.Pinned);
aStruct = (StructType) Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(StructType));
handle.Free();
But I noticed that any variable bigger then byte, like int32, int16 receives the data backwards. For example, if in the file the data was: AA BB CC DD the corresponding variable(int32) will be : DD CC BB AA.
My struct is defined with the attribute [StructLayout(LayoutKind.Sequential), Pack = 1]
Anyone knows why and how to solve it?
Thank you!