You may utilize the features of System.Runtime.InteropServices.Marshal:
byte [] buff = new byte [Marshal.SizeOf(typeof(MYSTRUCT))];
/* Put data into buffer here. */
IntPtr ptr = Marshal.AllocHGlobal(buff.Length);
Marshal.Copy(buff, 0x0, ptr, buff.Length);
MYSTRUCT myStruct = (MYSTRUCT) Marshal.PtrToStructure(ptr,
typeof(MYSTRUCT));
Marshal.FreeHGlobal(ptr);
If you are OK with using "unsafe" code, i think this is the superior
version
(you will have to compile with the /unsafe switch):
MYSTRUCT myStruct;
byte [] buff = new byte [sizeof(MYSTRUCT)];
/* Put data into buffer here. */
unsafe
{
fixed (byte * pBuff = buff)
myStruct = * ((MYSTRUCT *) pBuff);
}
Remember to explicitly specify that the members of the struct must not
be organized in another way than in the order that they are defined.
This is done with the System.Runtime.InteropServices.StructLayoutAttribute
attribute:
[ StructLayout( LayoutKind.Sequential, Pack = 0x1 ) ]
struct MYSTRUCT
{
...
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Amil Hanish said:
I have a huge byte array from another API that contains structs of
differing
sizes. I'd like to create n structs based on data in the byte array.
But
how? For example, if I want to take bytes 40-56 and create struct Foo
from
them...how do I do this? You can assume I know how to create the
properly
aligned struct.
Thanks
Amil