Exemple #1
0
        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
        }
Exemple #2
0
        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);
        }