[C#]结构体和字节数组的相互转化
2017-09-01 09:47
330 查看
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# 转换uint,byte[],char[],string, short[]<->byte[] 结构体和字节数组转化
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- C#中字符数组,字节数组和string之间的转化
- qsettings 保存自定义结构体(QVariant与自定义结构体相互转化)
- java时间的毫秒数和C#的datetime时间相互转化方法
- Matlab Simulink 串口通讯之float数据到字节数组(uint8)的相互转化
- C# 结构体和字节数组的转换
- C#中结构体和字节数组转换实现
- C#中英文逗号之间的相互转化
- c#编程指南(十七) 字符串和字节流相互转化
- C# 结构体定义 转换字节数组 z
- C#结构体和字节数组的转换
- 【C#】byte[]数组中有值大于127时与string之间的相互转化
- asp.net(C#)中进制之间相互转化
- (一)一个工作任务引起的乱战——c#中结构体与byte[]间相互转换
- C#结构体和字节数组的转换