How to convert a structure to a byte array in C#?

前端 未结 14 842
难免孤独
难免孤独 2020-11-22 09:59

How do I convert a structure to a byte array in C#?

I have defined a structure like this:

public struct CIFSPacket
{
    public uint protocolIdentifi         


        
14条回答
  •  难免孤独
    2020-11-22 10:39

            Header header = new Header();
            Byte[] headerBytes = new Byte[Marshal.SizeOf(header)];
            Marshal.Copy((IntPtr)(&header), headerBytes, 0, headerBytes.Length);
    

    This should do the trick quickly, right?

提交回复
热议问题