- 浏览: 4147337 次
最新评论
[C#]如何将自定义的structure转换为byte[]?
如何将自定义的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#中将byte数组转换为8bit灰度图像
C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线C# 绘制自定义虚线
C#用户自定义控件C#用户自定义控件C#用户自定义控件C#用户自定义控件
c#中string转换成字节byte的处理方法
C# 自定义控件 自定义ComboBox。其他控件的自定义与此类似。
内含一些自定义的C#控件,有demo,有源码,很不错的控件库,虽然不是很多,但是至少提供了一个学习的方法!
主要解决开发人员遇到的常规问题,针对 C# 结构体 和 Byte 数组之间互相转换遇到的问题,代码中进行了简单封装,方便客户调用,下载可以直接使用,方便快捷。
发送数据时,只能发送byte数组,处理起来比较麻烦不说,如果是和c++等写的程序通信的话,很多的都是传送结构体,在VC6.0中可以很方便的把一个char[]数组转换为一个结构体,而在C#却不能直接把byte数组转换为结构体,...
C#中字符串转换为计算公式-并进行计算的方法(自定义公式的计算)
C# string byte数组转换解析 C# string byte数组转换实现的过程是什么呢?C# string byte数组间的转换需要注意什么呢?C# string byte数组间转换所涉及的方法是什么呢?让我们来看看具体的内容: C# string byte数组...
C# WinForm 开发的自定义水晶控件!代码也是我在网上偶然发现的,特0分分享出来,让CS的童鞋们知道客户端程序也不再单一!
这是我用C#写的自定义组件和控件,内附一个testForm来演示如何使用。 其中,自定义的组件有:速选组件、窗口靠边停靠组件、窗口抖动组件。 自定义控件有:黑白的菜单栏、带进度显示的进度条等。
一个在VC中将Bitmap转换为Byte[]的小例子。
C# 自定义用户控件(PictureBox)
C# 自定义控件实例 C# 自定义控件实例 C# 自定义控件实例 button,Control,ProgressBar,tabControl, TrackBar
C#自定义控件.rar
我们会遇到很多的配置参数:网站名称,上传图片后缀,上传文件后缀,关键字过滤,数据库连接字串等等,这些内容如果比较少的话,直接配置到Web.config文件中,借由.NET提供的操作类,将会非常方便的来操作这些自定义配置节点...
c# winform 自定义折叠容器。有慢动画效果。
使用VS2005编写,VS2005以上的版本都可以使用此控件
自定义 Windows 窗体 DataGridView 控件 现有的几种嵌套控件,但无时间控件类型,下载代码中就是嵌套了时间控件类。 System.Windows.Forms.DataGridViewTextBoxColumn, System.Windows.Forms.DataGridView...