结构体和数据流的转换

转:https://www.cnblogs.com/lzy575566/p/7718815.html

c#结构体和字节流之间的相互转换

 

结构体转byte数组

1  首先要明白 ,是 在那个命名空间下  System.Runtime.InteropServices;

2  首先得到结构体的大小

2  开辟相应的内存空间

3  将结构体填充进开辟的内存空间

4  从内存空间拷贝进byte数组

5  不要忘记释放内存哦

复制代码
  public static byte[] StructToBytes(object structObj, int size = 0)
    {
        if (size == 0)
        {
            size = Marshal.SizeOf(structObj); //得到结构体大小
        }
        IntPtr buffer = Marshal.AllocHGlobal(size);  //开辟内存空间
        try
        {
            Marshal.StructureToPtr(structObj, buffer, false);   //填充内存空间
            byte[] bytes = new byte[size];
            Marshal.Copy(buffer, bytes, 0, size);   //填充数组
            return bytes;
        }
        catch (Exception ex)
        {
            Debug.LogError("struct to bytes error:" + ex);
            return null;
        }
        finally
        {
            Marshal.FreeHGlobal(buffer);   //释放内存
        }
    }
复制代码

同理,接受到的byte数组,转换为结构体

1  开辟内存空间

2  用数组填充内存空间

3  将内存空间的内容转换为结构体

4 同样不要忘记释放内存

复制代码
 public static object BytesToStruct(byte[] bytes, Type strcutType, int nSize)
    {
        if (bytes == null)
        {
            Debug.LogError("null bytes!!!!!!!!!!!!!");
        }
        int size = Marshal.SizeOf(strcutType);
        IntPtr buffer = Marshal.AllocHGlobal(nSize);
        //Debug.LogError("Type: " + strcutType.ToString() + "---TypeSize:" + size + "----packetSize:" + nSize);
        try
        {
            Marshal.Copy(bytes, 0, buffer, nSize);
            return Marshal.PtrToStructure(buffer, strcutType);
        }
        catch (Exception ex)
        {
            Debug.LogError("Type: " + strcutType.ToString() + "---TypeSize:" + size + "----packetSize:" + nSize);
            return null;
        }
        finally
        {
            Marshal.FreeHGlobal(buffer);
        }
    }
复制代码

结构体转byte数组

1  首先要明白 ,是 在那个命名空间下  System.Runtime.InteropServices;

2  首先得到结构体的大小

2  开辟相应的内存空间

3  将结构体填充进开辟的内存空间

4  从内存空间拷贝进byte数组

5  不要忘记释放内存哦

复制代码
  public static byte[] StructToBytes(object structObj, int size = 0)
    {
        if (size == 0)
        {
            size = Marshal.SizeOf(structObj); //得到结构体大小
        }
        IntPtr buffer = Marshal.AllocHGlobal(size);  //开辟内存空间
        try
        {
            Marshal.StructureToPtr(structObj, buffer, false);   //填充内存空间
            byte[] bytes = new byte[size];
            Marshal.Copy(buffer, bytes, 0, size);   //填充数组
            return bytes;
        }
        catch (Exception ex)
        {
            Debug.LogError("struct to bytes error:" + ex);
            return null;
        }
        finally
        {
            Marshal.FreeHGlobal(buffer);   //释放内存
        }
    }
复制代码

同理,接受到的byte数组,转换为结构体

1  开辟内存空间

2  用数组填充内存空间

3  将内存空间的内容转换为结构体

4 同样不要忘记释放内存

复制代码
 public static object BytesToStruct(byte[] bytes, Type strcutType, int nSize)
    {
        if (bytes == null)
        {
            Debug.LogError("null bytes!!!!!!!!!!!!!");
        }
        int size = Marshal.SizeOf(strcutType);
        IntPtr buffer = Marshal.AllocHGlobal(nSize);
        //Debug.LogError("Type: " + strcutType.ToString() + "---TypeSize:" + size + "----packetSize:" + nSize);
        try
        {
            Marshal.Copy(bytes, 0, buffer, nSize);
            return Marshal.PtrToStructure(buffer, strcutType);
        }
        catch (Exception ex)
        {
            Debug.LogError("Type: " + strcutType.ToString() + "---TypeSize:" + size + "----packetSize:" + nSize);
            return null;
        }
        finally
        {
            Marshal.FreeHGlobal(buffer);
        }
    }
复制代码

猜你喜欢

转载自www.cnblogs.com/judes/p/9052193.html