[C#]结构体和字节数组的相互转化
2017-09-01 09:47
295 查看
public static class StructCopyer { // 相当于序列化与反序列化,但是不用借助外部文件 //1、struct转换为Byte[] public static Byte[] StructToBytes(Object structure) { Int32 size = Marshal.SizeOf(structure); IntPtr buffer = Marshal.AllocHGlobal(size); try { Marshal.StructureToPtr(structure, buffer, false); Byte[] bytes = new Byte[size]; Marshal.Copy(buffer, bytes, 0, size); return bytes; } finally { Marshal.FreeHGlobal(buffer); } } //2、Byte[]转换为struct public static Object BytesToStruct(Byte[] bytes, Type strcutType) { Int32 size = Marshal.SizeOf(strcutType); IntPtr buffer = Marshal.AllocHGlobal(size); try { Marshal.Copy(bytes, 0, buffer, size); return Marshal.PtrToStructure(buffer, strcutType); } finally { Marshal.FreeHGlobal(buffer); } } }
相关文章推荐
- [C#]结构体和字节数组的相互转化
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- C# 转换uint,byte[],char[],string, short[]<->byte[] 结构体和字节数组转化
- C#结构体和字节数组的转换
- C#通讯编程入门--整形、双精度浮点型、字符串与字节型的相互转化
- C# byte[]和文件FileStream相互转化
- C#、Asp.Net 农历与阴历之间相互转化工具类
- C# Bitmap类型与Byte[]类型相互转化
- C#Image和Icon的相互转化
- 结构体和二进制数据相互转换c#(good)
- (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换
- C#结构体和字节数组的转换函数
- C#中字符数组,字节数组和string之间的转化
- C#中字符数组,字节数组和string之间的转化(转)
- asp.net(C#)中进制之间相互转化
- C# double float int string 与 byte数组 相互转化
- C# 时间戳与时间相互转化