简体   繁体   中英

unity c# byte array to struct contain data array

I want to convert byte array(size 38) to this struct.

when I edit code to public byte[] arrayAxis; this code run very wall.

please help... thank you!

[System.Serializable]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct SInfo
{
    public byte STX;
    public short Length;
    public short ID;
    public byte CMD;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
    public float[] arrAxis;
    //public float AxisX;
    //public float AxisY;
    //public float AxisZ;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
    public float[] arrQuat;
    //public float QuaternionX;
    //public float QuaternionY;
    //public float QuaternionZ;
    //public float QuaternionW;

    public byte State;
    public short State2;

    public byte CRC;
}

I wrote and used this function.

public T FromByteArray<T>(byte[] _array)
{
    BinaryReader _reader = new BinaryReader(new MemoryStream(_array));

    GCHandle handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
    T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
    handle.Free();

    return theStructure;
}

The size of the structure is 122 bytes because a float is 4 bytes. So arrAxis = 4 x 12 and arrQuat = 4 x 16. Code below is tested.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Runtime.InteropServices;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            SInfo s = new SInfo();
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(s));
            Marshal.StructureToPtr(s, ptr, true);
            byte[] buffer = new byte[Marshal.SizeOf(s)];
            Marshal.Copy(ptr, buffer, 0, Marshal.SizeOf(s));

            Test test = new Test();
            SInfo sinto = test.FromByteArray<SInfo>(buffer);


        }
    }
    public class Test
    {
        public T FromByteArray<T>(byte[] _array)
        {
            BinaryReader _reader = new BinaryReader(new MemoryStream(_array));

            GCHandle handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
            T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
            handle.Free();

            return theStructure;
        }
    }

    [System.Serializable]
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct SInfo
    {
        public byte STX;
        public short Length;
        public short ID;
        public byte CMD;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
        public float[] arrAxis;
        //public float AxisX;
        //public float AxisY;
        //public float AxisZ;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public float[] arrQuat;
        //public float QuaternionX;
        //public float QuaternionY;
        //public float QuaternionZ;
        //public float QuaternionW;

        public byte State;
        public short State2;

        public byte CRC;
    }


}

SizeConst Indicates the number of elements in the fixed-length array or the number of characters (not bytes) in a string to import.

So you probably wanted to set it to 3 and 4 instead of 12 and 16 respectively - then the size of this struct will be 38 bytes.

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