0

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;
}
1
  • What happens when you run this code - is there an exception? Do you have some sample input and expected output? Commented Sep 12, 2018 at 11:35

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }


}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.