C#中struct的字節對齊、轉換操作和復制為二進制數據(byte[])


在做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;
            public byte 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());

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM