现在的位置: 首页 > 综合 > 正文

C#中struct的字节对齐、转换操作和复制为二进制数据(byte[])

2013年10月14日 ⁄ 综合 ⁄ 共 2184字 ⁄ 字号 评论关闭

在做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中定义定长的字段,在解析数据包时动态截取字符串。

二、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());

抱歉!评论已关闭.