public unsafe static void BlitBytes <T>(T value, byte[] bytes = null, int offset = 0) where T : struct { if (bytes == null) bytes = new byte[offset + Marshal.SizeOf(value)]; fixed(byte *lpBytes = &bytes[offset]) { Marshal.StructureToPtr(value, new IntPtr(lpBytes), false); } ByteOrder.Convert(typeof(T), bytes, offset); // swap endian if needed }
public unsafe static T BlitStruct <T>(byte[] bytes, int offset = 0) where T : struct { T value = default(T); Type type = typeof(T); bytes = ByteOrder.Convert(type, bytes, offset); // swap endian if needed fixed(byte *lpBytes = &bytes[offset]) { value = (T)Marshal.PtrToStructure(new IntPtr(lpBytes), type); } return(value); }