日期:2014-05-18  浏览次数:20866 次

C# 通讯时字节流和结构体互转

//Author:smilelance

//From:http://blog.csdn.net/smilelance

//转换C#代码:

//结构体转换成字节流

public static byte[] StructToBytes<T>(T obj)
    {
int size = Marshal.SizeOf(typeof(T));
   IntPtr bufferPtr = Marshal.AllocHGlobal(size);
try   
{
       Marshal.StructureToPtr(obj, bufferPtr, false);
       byte[] bytes = new byte[size];
       Marshal.Copy(bufferPtr, bytes, 0, size);

       return bytes;
}
        catch(Exception ex)
        {
            throw new Exception("Error in StructToBytes ! " + ex.Message);
        }
finally   
{   
  Marshal.FreeHGlobal(bufferPtr);   
}  
    }


//字节流转换成结构体

    public static T BytesToStruct<T>(byte[] bytes, int startIndex = 0)
    {
        if (bytes == null) return default(T);
        if (bytes.Length <= 0) return default(T);
int objLength = Marshal.SizeOf(typeof(T));
        IntPtr bufferPtr = Marshal.AllocHGlobal(objLength);
        try//struct_bytes转换
        {
            Marshal.Copy(bytes, startIndex, bufferPtr, objLength);
            return (T)Marshal.PtrToStructure(bufferPtr, typeof(T));
        }
        catch(Exception ex)
        {
            throw new Exception("Error in BytesToStruct ! " + ex.Message);
        }
        finally
        {
            Marshal.FreeHGlobal(bufferPtr);
        }
    }


[StructLayout(LayoutKind.Sequential, Pack=1)]  //变量在内存中的对齐方式 
public struct LolMsgHeader
{
public ushort wMsgLen;
public byte    header_ver;
public ushort  uAction; //动作行为
public uint  dwUid; //用户ID
public uint dwSeq; //包的序列号