c#中关于结构体和字节数组转化
来源:互联网 发布:黛堡嘉莱价格淘宝 编辑:程序博客网 时间:2024/05/17 23:55
最近在使用结构体与字节数组转化来实现socket间数据传输。现在开始整理一下。对于Marshal可以查阅msdn,关于字节数组与结构体转代码如下:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Runtime.InteropServices;
namespace
FileSendClient
{
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct
StructDemo
{
public
byte
a;
public
byte
c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public
byte
[] b;
public
byte
d;
public
int
e;
}
unsafe
class
Program
{
static
void
Main(
string
[] args)
{
StructDemo sd;
sd.a = 0;
sd.d = 0;
sd.c = 0;
sd.b =
new
byte
[3] { 0, 0, 1 };
sd.e = 5;
int
size = 0;
//此处使用非安全代码来获取到StructDemo的值
unsafe
{
size = Marshal.SizeOf(sd);
}
byte
[] b = StructToBytes(sd,size);
ByteToStruct(b,
typeof
(StructDemo));
}
//将Byte转换为结构体类型
public
static
byte
[] StructToBytes(
object
structObj,
int
size)
{
StructDemo sd;
int
num = 2;
byte
[] bytes =
new
byte
[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将结构体拷到分配好的内存空间
Marshal.StructureToPtr(structObj, structPtr,
false
);
//从内存空间拷贝到byte 数组
Marshal.Copy(structPtr, bytes, 0, size);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return
bytes;
}
//将Byte转换为结构体类型
public
static
object
ByteToStruct(
byte
[] bytes, Type type)
{
int
size = Marshal.SizeOf(type);
if
(size > bytes.Length)
{
return
null
;
}
//分配结构体内存空间
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将byte数组拷贝到分配好的内存空间
Marshal.Copy(bytes, 0, structPtr, size);
//将内存空间转换为目标结构体
object
obj = Marshal.PtrToStructure(structPtr, type);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return
obj;
}
}
}
最近在使用结构体与字节数组转化来实现socket间数据传输。现在开始整理一下。对于Marshal可以查阅msdn,关于字节数组与结构体转代码如下:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Runtime.InteropServices;
namespace
FileSendClient
{
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct
StructDemo
{
public
byte
a;
public
byte
c;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public
byte
[] b;
public
byte
d;
public
int
e;
}
unsafe
class
Program
{
static
void
Main(
string
[] args)
{
StructDemo sd;
sd.a = 0;
sd.d = 0;
sd.c = 0;
sd.b =
new
byte
[3] { 0, 0, 1 };
sd.e = 5;
int
size = 0;
//此处使用非安全代码来获取到StructDemo的值
unsafe
{
size = Marshal.SizeOf(sd);
}
byte
[] b = StructToBytes(sd,size);
ByteToStruct(b,
typeof
(StructDemo));
}
//将Byte转换为结构体类型
public
static
byte
[] StructToBytes(
object
structObj,
int
size)
{
StructDemo sd;
int
num = 2;
byte
[] bytes =
new
byte
[size];
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将结构体拷到分配好的内存空间
Marshal.StructureToPtr(structObj, structPtr,
false
);
//从内存空间拷贝到byte 数组
Marshal.Copy(structPtr, bytes, 0, size);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return
bytes;
}
//将Byte转换为结构体类型
public
static
object
ByteToStruct(
byte
[] bytes, Type type)
{
int
size = Marshal.SizeOf(type);
if
(size > bytes.Length)
{
return
null
;
}
//分配结构体内存空间
IntPtr structPtr = Marshal.AllocHGlobal(size);
//将byte数组拷贝到分配好的内存空间
Marshal.Copy(bytes, 0, structPtr, size);
//将内存空间转换为目标结构体
object
obj = Marshal.PtrToStructure(structPtr, type);
//释放内存空间
Marshal.FreeHGlobal(structPtr);
return
obj;
}
}
}
0 0
- c#中关于结构体和字节数组转化
- c#中关于结构体和字节数组转化
- C#中结构体和byte数组的转化
- C# 转换uint,byte[],char[],string, short[]<->byte[] 结构体和字节数组转化
- [C#]结构体和字节数组的相互转化
- C#中字符数组,字节数组和string之间的转化
- C#中字符数组,字节数组和string之间的转化
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换
- C# 结构体和字节数组的转换
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换
- C#结构体和字节数组的转换函数
- C# 结构体和字节数组的转换
- 视频剪切软件BandiCut
- 第一个swift程序
- 关于QEMU中的类型算法
- Emacs中文识别以及中文输入法的相关问题
- UVA 10635 Prince and Princess lcs--》lis
- c#中关于结构体和字节数组转化
- C++ stl set用法例子
- 将el表达式list遍历的的数值,传递到jquery的click函数里面,实现删除
- Android开发工具的下载
- 九宫格dfs
- c++ STL学习笔记
- hdu 1058 Humble Numbers
- Redis参数配置说明
- C++反射的实现