请问如何在C#中将int类型的变量拷贝到byte数组中,即实现C中如下的功能: Struct SendBuf
{
Char tb[3];
Char bc[2];
Char buf[512];
} TxnBuf;
int tt=63;
tt=htons(tt);
TxnBuf.tb[0]=2;
TxnBuf.tb[1]=0;
TxnBuf.tb[2]=0;
memcpy(TxnBuf.bc,&tt,2); //关键是这里麻烦大家乐
{
Char tb[3];
Char bc[2];
Char buf[512];
} TxnBuf;
int tt=63;
tt=htons(tt);
TxnBuf.tb[0]=2;
TxnBuf.tb[1]=0;
TxnBuf.tb[2]=0;
memcpy(TxnBuf.bc,&tt,2); //关键是这里麻烦大家乐
在定类等前面加上这个
private unsafe
System.Runtime.InteropServices.Marshal.WriteInt16(p, (short)tt);