To read binary data into a structure in C#, you will need to use the BitConverter class and some pointer magic using the IntPtr type. Here's an example for reading a struct named "MyStruct" from a byte array:
main.cs884 chars29 lines
This code first gets the size of the struct using the Marshal.SizeOf method. It then makes sure that the binary data is at least as large as the struct, and allocates a buffer of that size using Marshal.AllocHGlobal. The binary data is then copied into the buffer using Marshal.Copy. Finally, the struct is read from the buffer using Marshal.PtrToStructure, and the allocated memory is freed using Marshal.FreeHGlobal.
Note that this method does not pack the struct, which means that the memory layout will be identical to the C/C++ equivalent of the struct.
gistlibby LogSnag