日期:2014-05-17 浏览次数:21264 次
byte[] mytest = new byte[4];
mytest[0]=0x01;
mytest[1]=0x02;
mytest[2]=0x03;
mytest[3]=0x04;
//实现4字节数组到整数的转换
b = mytest[3] + (mytest[2] << 8) + (mytest[1] << 16)+(mytest[0] << 24);
byte[] buffer = BitConverter.GetBytes(1000);
/// <summary>
/// 自定义结构转换为byte[]类型
/// </summary>
/// <param name="anything">自定义结构</param>
/// <returns>byte[]数组</returns>
public static byte[] RawSerialize(object anything)
{
int rawsize = Marshal.SizeOf(anything);
IntPtr buffer = Marshal.AllocHGlobal(rawsize);
Marshal.StructureToPtr(anything, buffer, false);
byte[] rawdatas = new byte[rawsize];
Marshal.Copy(buffer, rawdatas, 0, rawsize);