请教C#下如何实现自定义结构或类与BYTE数组之间的相互转换(即数据的打包与解包)? 在C#定义类似如下的结构和类: [Serializable] [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct STCord { public double x; // public double y; public byte type; public int num; }; [Serializable] [StructLayout(LayoutKind.Sequential, Pack = 1)] public class STPara { public byte type1; // public int[] fre = new int[5]; // public byte type2; // }; [Serializable] [StructLayout(LayoutKind.Sequential, Pack = 1)] // public class STChar { public byte num; // public double Time; // public STCord Pos; // public int Pw; // public STPara Para; // public byte type; // public STTargetCharacter() { Para= new STPara(); } }; 在VC6中定义的结构为: struct STCord { double x; // double y; byte type; int num; };
struct STPara { public byte type1; // public int fre[5]; // public byte type2; // }; struct STChar { byte num; // double Time; // STCord Pos; // int Pw; // STPara Para; // byte type; // }; VC6中定义: STChar stDataVC6,BYTE数组: BYTE bufVC6[1024]; VS2005中定义:STChar stDataVS05=new STChar(); BYTE bufVS05=new BYTE[1024]; 如果是VC6与VC6之间利用UDP/TCP进行数据传输,则只需要将stDataVC6与收到的BYTE数组bufVC6利用memcpy指令进行转换就可得到所