Revision: 26204
Updated Code
at June 18, 2010 06:10 by pckujawa
Updated Code
public void Main() { var beBytes = new byte[] { 0x80, 0x80, 0x80, 0x80, 0x80,0, 0x80,0, 0x80,0,0,0, 0x80,0,0,0, 0x80,0,0,0,0,0,0,0, 0x80,0,0,0,0,0,0,0, // 0x3F,0x80,0,0, // 0x3F,0xF0,0,0,0,0,0,0, // 0x30,0,0,0,0,0,0,0,0,0, 0x54,0x65,0x73,0x74,0x69,0x6E,0x67,0,0,0 }; var leBytes = new byte[] { 0x80, 0x80, 0x80, 0x80, 0,0x80, 0,0x80, 0,0,0,0x80, 0,0,0,0x80, 0,0,0,0,0,0,0,0x80, 0,0,0,0,0,0,0,0x80, // 0,0,0x80,0x3F, // 0,0,0,0,0,0,0xF0,0x3F, // 0x30,0,0,0,0,0,0,0,0,0, 0x54,0x65,0x73,0x74,0x69,0x6E,0x67,0,0,0 }; Foo fooLe = ByteArrayToStructure<Foo>(leBytes).Dump("LE"); Foo fooBe = ByteArrayToStructureBigEndian<Foo>(beBytes, "bbbbsSiIlL" // + "fd" +"9bb").Dump("BE"); // Assert.AreEqual(fooLe, fooBe); } [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Foo { public byte b1; public byte b2; public byte b3; public byte b4; public short s; public ushort S; public int i; public uint I; public long l; public ulong L; // public float f; // public double d; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string MyString; } T ByteArrayToStructure<T>(byte[] bytes) where T: struct { GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T)); handle.Free(); return stuff; } T ByteArrayToStructureBigEndian<T>(byte[] bytes, string description) where T: struct { byte[] buffer = bytes; IList unpacked = DataConverter.Unpack("^"+description, buffer, 0).Dump("unpacked"); buffer = DataConverter.PackEnumerable("!"+description, unpacked).Dump("packed"); return ByteArrayToStructure<T>(buffer); }
Revision: 26203
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 20, 2010 16:03 by pckujawa
Initial Code
public void Main() { var beBytes = new byte[] { 0x80, 0x80, 0x80, 0x80, 0x80,0, 0x80,0, 0x80,0,0,0, 0x80,0,0,0, 0x80,0,0,0,0,0,0,0, 0x80,0,0,0,0,0,0,0, // 0x3F,0x80,0,0, // 0x3F,0xF0,0,0,0,0,0,0, // 0x30,0,0,0,0,0,0,0,0,0, 0x54,0x65,0x73,0x74,0x69,0x6E,0x67,0,0,0 }; var leBytes = new byte[] { 0x80, 0x80, 0x80, 0x80, 0,0x80, 0,0x80, 0,0,0,0x80, 0,0,0,0x80, 0,0,0,0,0,0,0,0x80, 0,0,0,0,0,0,0,0x80, // 0,0,0x80,0x3F, // 0,0,0,0,0,0,0xF0,0x3F, // 0x30,0,0,0,0,0,0,0,0,0, 0x54,0x65,0x73,0x74,0x69,0x6E,0x67,0,0,0 }; Foo fooLe = ByteArrayToStructure<Foo>(leBytes).Dump("LE"); Foo fooBe = ByteArrayToStructureBigEndian<Foo>(beBytes, "bbbbsSiIlL" // + "fd" +"9bb").Dump("BE"); // Assert.AreEqual(fooLe, fooBe); } [StructLayout(LayoutKind.Sequential)] public struct Foo { public byte b1; public byte b2; public byte b3; public byte b4; public short s; public ushort S; public int i; public uint I; public long l; public ulong L; // public float f; // public double d; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)] public string MyString; } T ByteArrayToStructure<T>(byte[] bytes) where T: struct { GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T)); handle.Free(); return stuff; } T ByteArrayToStructureBigEndian<T>(byte[] bytes, string description) where T: struct { byte[] buffer = bytes; IList unpacked = DataConverter.Unpack("^"+description, buffer, 0).Dump("unpacked"); buffer = DataConverter.PackEnumerable("!"+description, unpacked).Dump("packed"); return ByteArrayToStructure<T>(buffer); }
Initial URL
http://stackoverflow.com/questions/2480116/marshalling-a-big-endian-byte-collection-into-a-struct-in-order-to-pull-out-value/2678472#2678472
Initial Description
See the URL for my question and answer on StackOverflow.
Initial Title
Byte Swapping Structs For Conversion From Big To Little Endian (Byte Order)
Initial Tags
Initial Language
C#