`
阿尔萨斯
  • 浏览: 4147337 次
社区版块
存档分类
最新评论

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

 
阅读更多
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>

如何将自定义的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




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics