简体   繁体   中英

Converting struct into byte[] in c#

I have this struct inside a class:

[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)]
public struct MyStruct
{
    public byte StartOfText;
    public byte DisableChecksum;
    public byte ProtocolVersion;
    public byte Code;
    public Int16 Size;
    public byte[] Data;
    public byte EndOfText;

    public MyStruct(CommandCode commandCode, string commandData)
    {
        this.StartOfText = 0x02;
        this.DisableChecksum = 0x00;
        this.ProtocolVersion = 0x35;
        this.Code = (byte)commandCode;
        this.Size = (Int16)commandData.Length;
        this.Data = new byte[commandData.Length];
        this.Data = Encoding.ASCII.GetBytes(commandData);
        this.EndOfText = 0x03;
    }

    public byte[] ToByteArray()
    {
        byte[] arr = null;
        IntPtr ptr = IntPtr.Zero;
        try
        {
            Int16 size = (Int16)Marshal.SizeOf(this);
            arr = new byte[size];
            ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(this, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            Marshal.FreeHGlobal(ptr);
        }

        return arr;
    }
}

Now, suppose I initialise a new instance of that struct in my program doing the following:

Protocol.MyStruct Cmd = new Protocol.MyStruct(CommandCode.LOAD_FILE, "TEST");
byte[] StructData = Cmd.ToByteArray();

When initialized, I can see that MyStruct.Data is being initialized properly with the content of the string constant (0x54, 0x45, 0x53, 0x54). Each characters

But when the MyStruct.TiByteArray() is called and I trace the values in the array, the Data part is now (0xa0, 0xa6, 0x91 and 0x03).

The rest of the array is ok and data is copied correctly.

What is wrong with the code ou what am I missing?

This may work in some situations. However, the length of Data is fixed. It may be more useful if Data is defined as string.

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct MyStruct
{
    const int ARRAY_SIZE = 100;

    public byte StartOfText;
    public byte DisableChecksum;
    public byte ProtocolVersion;
    public byte Code;
    public Int16 Size;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = ARRAY_SIZE)]
    public byte[] Data;
    public byte EndOfText;

    public MyStruct(CommandCode commandCode, string commandData)
    {
        this.StartOfText = 0x02;
        this.DisableChecksum = 0x00;
        this.ProtocolVersion = 0x35;
        this.Code = (byte)commandCode;
        this.Size = (Int16)commandData.Length;
        this.Data = new byte[ARRAY_SIZE];
        byte[] bytes = Encoding.ASCII.GetBytes(commandData);
        Array.Copy(bytes, Data, bytes.Length);
        //this.Data = Encoding.ASCII.GetBytes(commandData);
        this.EndOfText = 0x03;
    }

    public byte[] ToByteArray()
    {
        byte[] arr = null;
        IntPtr ptr = IntPtr.Zero;
        try
        {
            int size = Marshal.SizeOf(this);
            arr = new byte[size];
            ptr = Marshal.AllocHGlobal(size);
            Marshal.StructureToPtr(this, ptr, true);
            Marshal.Copy(ptr, arr, 0, size);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            Marshal.FreeHGlobal(ptr);
        }

        return arr;
    }
}

Add SerializableAttribute at the top of your Struct declaration and substitute the ToByteArray() method, like this:

[StructLayout(LayoutKind.Sequential, Pack = 1), Serializable]
public struct MyStruct
{
    public byte StartOfText;
    public byte DisableChecksum;
    public byte ProtocolVersion;
    public byte Code;
    public Int16 Size;
    public byte[] Data;
    public byte EndOfText;

    public MyStruct(CommandCode commandCode, string commandData)
    {
        this.StartOfText = 0x02;
        this.DisableChecksum = 0x00;
        this.ProtocolVersion = 0x35;
        this.Code = (byte)commandCode;
        this.Size = (Int16)commandData.Length;
        this.Data = new byte[commandData.Length];
        this.Data = Encoding.ASCII.GetBytes(commandData);
        this.EndOfText = 0x03;
    }

    public byte[] ToByteArray()
    {
        BinaryFormatter formatter = null;
        MemoryStream stream = null;
        byte[] arr = null;
        try
        {
            formatter = new BinaryFormatter();
            stream = new MemoryStream();
            formatter.Serialize(stream, this);
            arr = stream.ToArray();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
        finally
        {
            if(null != stream)
            {
                stream.Dispose();
            }
        }
        return arr;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM