[C#]如何将自定义的structure转换为byte[]?

本文介绍了一种使用 C# 的 Marshal 类将自定义结构体转换为字节数组的方法,并提供了序列化和反序列化的示例代码。

如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




如何将自定义的structure转换为byte[]

整理者:郑昀@UltraPower
示例如下:
using System.Runtime.InteropServices;

        public static byte[] RawSerialize( object anything )

        {

            int rawsize = Marshal.SizeOf( anything );

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.StructureToPtr( anything, buffer, false );

            byte[] rawdatas = new byte[ rawsize ];

            Marshal.Copy( buffer, rawdatas, 0, rawsize );

            Marshal.FreeHGlobal( buffer );

            return rawdatas;

        }

 

        public static object RawDeserialize( byte[] rawdatas, Type anytype )

        {

            int rawsize = Marshal.SizeOf( anytype );

            if( rawsize > rawdatas.Length )

                return null;

            IntPtr buffer = Marshal.AllocHGlobal( rawsize );

            Marshal.Copy( rawdatas, 0, buffer, rawsize );

            object retobj = Marshal.PtrToStructure( buffer, anytype );

            Marshal.FreeHGlobal( buffer );

            return retobj;

                 }

 

        // sample usage:

        [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]

            struct YourStruct

        {

            public Int32  First;

            public Int32  Second;

            [MarshalAs( UnmanagedType.ByValTStr, SizeConst=16 )]

            public String Text;

        }

 

        YourStruct st;

        st.First  = 11;

        st.Second = 22;

        st.Text = "Hello";

        byte[] rd = RawSerialize( st );

        // reverse:

        YourStruct rst = (YourStruct) RawDeserialize( rd, typeof(YourStruct));

整理者:郑昀@UltraPower




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值