[C#]如何将自定义的structure转换为byte[]?
来源:互联网 发布:c语言void用法 编辑:程序博客网 时间:2024/06/09 21:01
如何将自定义的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
- [C#]如何将自定义的structure转换为byte[]?
- 如何将自定义的structure转换为byte数组(VB.Net)
- C# 中将自定义的structure转换为byte[] 及Stream 和 byte[] 之间的转换(文件流的应用)
- 如何将YUV的byte数组转换为bitmap
- Delphi如何将byte型的数组转换为字符串
- [C#] 如何把void*转换为byte[]
- java 如何将byte中的有效长度转换为String
- 如何:将 char * 字符串转换为 System::Byte 数组
- java中如何将byte数组内容转换为字符串?
- java 如何将byte中的有效长度转换为String
- 将byte[]转换为float
- Java中如何将以byte数组给出的数据转换为double数组形式
- Chap8:如何将java传递过来的jbyteArray转换成C/C++中的BYTE数组
- 如何将LabVIEW的VI转换为C语言代码
- 如何快速将vc++的类转换为c#/cli
- C#.NET开发Winform使用MySQL数据库 无法将“System.Byte[]”的对象强制转换为类型“System.IConvertible”
- 如何将格式为byte[]的图片保存在本地
- 【C语言】【unix c】将自定义转换为环境变量
- [EnterpriseServices]利用assembly定义我们的组件在COM+中的注册方式
- a gcc compiler optimization problem from my colleague
- [C#]I/O完成端口的实现
- 离开几天
- [GOLF]过了磨合期了
- [C#]如何将自定义的structure转换为byte[]?
- 超级卓越企业
- Proxy Pattern (代理模式)
- [dotNET]“ThreadPool 对象中没有足够的自由线程来完成操作”的现象和解决办法
- linker error: unable to open input file 'coh.obj'错误的排除
- mysql,phpmyadmin 的安装 时遇到的问题[1251 - Client does yAdmin tried to connect
- 图形图像处理-之-一个复杂度为常数的快速局部自适应算法 上篇
- C#实现EXCEL数据转换到TXT文档
- TemplateWorld-网页模板下载