C#下,struct与字节数组的相互转换
来源:互联网 发布:上海跳跃网络吧爆吧 编辑:程序博客网 时间:2024/05/21 09:38
public struct TControlRequest
{
public int Code; // command code
public int UserID; // user id, 0=inner system
public int DevID; // device id
public unsafe fixed char Param[200]; // parament
};
{
public int Code; // command code
public int UserID; // user id, 0=inner system
public int DevID; // device id
public unsafe fixed char Param[200]; // parament
};
public struct TControlResponse
{
public int Result; // result
public unsafe fixed char Param[200]; // parament of result
};
{
public int Result; // result
public unsafe fixed char Param[200]; // parament of result
};
//将TControlRequest转换为字节数组
private byte[] TControlRequestToBytes(TControlRequest request)
{
int iSize = Marshal.SizeOf(typeof(TControlRequest));
IntPtr pCertCont = Marshal.AllocHGlobal(iSize);
Marshal.StructureToPtr(request, pCertCont, true);
{
int iSize = Marshal.SizeOf(typeof(TControlRequest));
IntPtr pCertCont = Marshal.AllocHGlobal(iSize);
Marshal.StructureToPtr(request, pCertCont, true);
byte[] myWriteBuffer = new byte[iSize];
Marshal.Copy(pCertCont, myWriteBuffer, 0, iSize);
Marshal.Copy(pCertCont, myWriteBuffer, 0, iSize);
return myWriteBuffer;
}
}
//将字节数组转换为TControlResponse
private TControlResponse BytesToTcontrolResponse(byte[] myReadBuffer)
{
TControlResponse response = new TControlResponse();
{
TControlResponse response = new TControlResponse();
try
{
int iSize = Marshal.SizeOf(typeof(TControlResponse));
IntPtr pCertCont = Marshal.AllocHGlobal(iSize);
{
int iSize = Marshal.SizeOf(typeof(TControlResponse));
IntPtr pCertCont = Marshal.AllocHGlobal(iSize);
Marshal.Copy(myReadBuffer, 0, pCertCont, iSize);
response = (TControlResponse)Marshal.PtrToStructure(pCertCont, typeof(TControlResponse));
}
catch(ArgumentException)
{
}
}
catch(ArgumentException)
{
}
return response;
}
}
- C#下,struct与字节数组的相互转换
- C#下字符串与字节数组之间的相互转换
- 整形变量与字节数组的相互转换
- 整型变量与字节数组的相互转换
- 浮点数与字节数组的相互转换
- 整型变量(int)与字节数组(byte[])的相互转换
- 字节数组与十六进制字符串之间的相互转换
- Java 字节数组与String的相互转换错误
- int long String与字节数组的相互转换
- 字符串与字节数组相互转换
- C# 字符串与字节数组相互转换
- Java 对象与字节数组相互转换
- 字符串与字节码的相互转换
- 字节数组与整数的相互转化
- [C#]image与byte数组的相互转换
- C#.NET中string与byte数组的相互转换
- C#:Image与byte数组的相互转换
- C#下byte数组与string的相互转换
- 微软公司 BizTalk Server 2006 R2 产品价格与许可常见问题
- 从emacs开始
- Maemo Linux手机平台系列分析:9 Maemo平台开发之 使用D-Bus信号做异步操作
- ajax+ajax futures+asp.net 2.0bug cannot create page method
- Session丢失问题
- C#下,struct与字节数组的相互转换
- Visual C#的剪切板編程
- Session持久化问题[转]
- 在IE下面指定表单编码方式
- const的使用
- 学习机票:国航怎么就有7折航班
- 基于 JSF+Spring + JPA 构建敏捷的Web应用[88250原创]
- 冬季恋歌
- 在 appSettings 里配置字符串,不在 connectionStrings 里写连接数据库的配置