C#中struct的位元組對齊、轉換操作和複製為二進位制資料(byte[])
阿新 • • 發佈:2019-02-13
在做C#與其它程式通訊的系統時,往往會使用struc操作結構化的資料(如資料包等)。
本文簡要提出一些使用思路,歡迎各位大牛賜教。
一、STRUCT結構設計
當資料的結構確定時,總結為下面兩種情況:
1、資料長度確定(包括字串):
此時可以直接利用struct來構造資料包,比如:
[StructLayout(LayoutKind.Sequential, Pack = 1)] struct THeader { public short size; public byte type;public int seqno; } [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)] struct TGSUpdateLadder { public THeader h; public byte charlevel; public uint charexplow; public uint charexphigh; publicbyte charclass; public ushort charstatus; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)] public string charname; }
StructLayout用來確定佈局方式,其中的Sequential表示在記憶體中按位元組對齊連續儲存,Pack指定位元組對齊方式(即幾字節對齊),CharSet用來指定ByValTStr等字串型別在複製到非託管記憶體(或從非託管記憶體中複製)時使用的字符集。
MarshalAs用來指明下一個欄位在複製到非託管區域(或從非託管記憶體中複製)時的轉換方式和長度。
除ByValTStr外,常用的還有:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 6, ArraySubType = UnmanagedType.SysInt)] public int[] reserved;
ByValArray可用來轉換一個長度確定的陣列,SizeConst指明陣列元素個數,ArraySubType指明陣列中每個元素的型別。
2、資料中字串長度不確定:
有時通訊中的字串沒有固定長度,在包中以\0結尾,這時如果非要使用struct表示,可以使用CustomMarshaler,但這將導致該struct在複製到非託管記憶體或從非託管記憶體複製時無法確定struct的長度(所有CustomMarshaler的長度都不可計算,不知道這一設計的原因是什麼,但可以手動計算),在此省略。
臨時解決辦法是僅在struct中定義定長的欄位,在解析資料包時動態擷取字串。
public byte[] StructToBytes(object obj) { int rawsize = Marshal.SizeOf(obj); IntPtr buffer = Marshal.AllocHGlobal(rawsize); Marshal.StructureToPtr(obj, buffer, false); byte[] rawdatas = new byte[rawsize]; Marshal.Copy(buffer, rawdatas, 0, rawsize); Marshal.FreeHGlobal(buffer); return rawdatas; } public object BytesToStruct(byte[] buf, int len, Type type) { object rtn; IntPtr buffer = Marshal.AllocHGlobal(len); Marshal.Copy(buf, 0, buffer, len); rtn = Marshal.PtrToStructure(buffer, type); Marshal.FreeHGlobal(buffer); return rtn; } public void BytesToStruct(byte[] buf, int len, object rtn) { IntPtr buffer = Marshal.AllocHGlobal(len); Marshal.Copy(buf, 0, buffer, len); Marshal.PtrToStructure(buffer, rtn); Marshal.FreeHGlobal(buffer); } public void BytesToStruct(byte[] buf, object rtn) { BytesToStruct(buf, buf.Length, rtn); } public object BytesToStruct(byte[] buf, Type type) { return BytesToStruct(buf, buf.Length, type); }
上面的程式碼可以將struct根據記憶體佈局方式轉換為byte[],或從byte[]轉換為特定型別的struct。
呼叫方式如:
byte[] SendBuf = StructToBytes(rpacket);
TGSGetDataRequest packet = new TGSGetDataRequest(); packet = (TGSGetDataRequest)BytesToStruct((byte[])buf, Marshal.SizeOf(packet), packet.GetType());