Andreas
2/2/2007 12:45:00 PM
Hi, Im filling a known structure with data from a byte-array and I know that
the values are represented in big-endian. Would it be possible to make this
conversion "on the fly" ? Example structure
[StructLayout(LayoutKind.Sequential)]
public struct MyStructure
{
public ushort Width;
public ushort Height;
}
Im using this code to populate the struct with data from a byte-array thats
retrieved from a filestream
public static T ReadStruct<T>(FileStream fs, int offset)
{
byte[] buffer =
new byte[Marshal.SizeOf(typeof(T))];
fs.Position = offset;
fs.Read(buffer, 0, Marshal.SizeOf(typeof(T)));
GCHandle handle =
GCHandle.Alloc(buffer, GCHandleType.Pinned);
T populatedItem =
(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();
return populatedItem;
}
It used like this
MyStruct s = ReadStruct<MyStruct>(fs, 0);
Thanks!
//Andreas