统一c#字节数组到结构包含数据数组

统一c#字节数组到结构包含数据数组,c#,arrays,unity3d,struct,C#,Arrays,Unity3d,Struct,我想把字节数组(大小38)转换成这个结构 当我将代码编辑为 公共字节[]arrayAxis; 这段代码在墙上运行得非常好 请帮忙。。。谢谢大家! [System.Serializable] [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct SInfo { public byte STX; public short Length; public short ID; public byte CM

我想把字节数组(大小38)转换成这个结构

当我将代码编辑为 公共字节[]arrayAxis; 这段代码在墙上运行得非常好

请帮忙。。。谢谢大家!

[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;
}
我编写并使用了这个函数

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;
}
public T FromByteArray(字节[]\u数组)
{
BinaryReader_reader=新的BinaryReader(新的内存流(_数组));
GCHandle=GCHandle.Alloc(_数组,GCHandleType.pinted);
T结构=(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();
返回结构;
}

结构的大小是122字节,因为浮点是4字节。所以arrAxis=4x12,arrQuat=4x16。下面的代码已经过测试

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


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.Runtime.InteropServices;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
SInfo s=新SInfo();
IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(s));
Marshal.StructureToPtr(s,ptr,true);
byte[]buffer=新字节[Marshal.SizeOf(s)];
Marshal.Copy(ptr,buffer,0,Marshal.SizeOf);
测试=新测试();
SInfo sinto=从字节数组(缓冲区)开始的测试;
}
}
公开课考试
{
公共T FromByteArray(字节[]_数组)
{
BinaryReader_reader=新的BinaryReader(新的内存流(_数组));
GCHandle=GCHandle.Alloc(_数组,GCHandleType.pinted);
T结构=(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();
返回结构;
}
}
[系统可序列化]
[StructLayout(LayoutKind.Sequential,Pack=1)]
公共结构信佛
{
公共字节STX;
公共短长度;
公共短ID;
公共字节CMD;
[Marshallas(UnmanagedType.ByValArray,SizeConst=12)]
公开发行【】arrAxis;
//公开发行AxisX;
//公共浮动轴;
//公共浮动AxisZ;
[Marshallas(UnmanagedType.ByValArray,SizeConst=16)]
公共浮动[]阿夸特;
//公共浮点四元数x;
//公共浮动四元数;
//公共浮点四元数z;
//公共浮动四元数w;
公共字节状态;
公共短期状态2;
公共字节CRC;
}
}

结构的大小是122字节,因为浮点是4字节。所以arrAxis=4x12,arrQuat=4x16。下面的代码已经过测试

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


}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用System.Runtime.InteropServices;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
SInfo s=新SInfo();
IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(s));
Marshal.StructureToPtr(s,ptr,true);
byte[]buffer=新字节[Marshal.SizeOf(s)];
Marshal.Copy(ptr,buffer,0,Marshal.SizeOf);
测试=新测试();
SInfo sinto=从字节数组(缓冲区)开始的测试;
}
}
公开课考试
{
公共T FromByteArray(字节[]_数组)
{
BinaryReader_reader=新的BinaryReader(新的内存流(_数组));
GCHandle=GCHandle.Alloc(_数组,GCHandleType.pinted);
T结构=(T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(),typeof(T));
handle.Free();
返回结构;
}
}
[系统可序列化]
[StructLayout(LayoutKind.Sequential,Pack=1)]
公共结构信佛
{
公共字节STX;
公共短长度;
公共短ID;
公共字节CMD;
[Marshallas(UnmanagedType.ByValArray,SizeConst=12)]
公开发行【】arrAxis;
//公开发行AxisX;
//公共浮动轴;
//公共浮动AxisZ;
[Marshallas(UnmanagedType.ByValArray,SizeConst=16)]
公共浮动[]阿夸特;
//公共浮点四元数x;
//公共浮动四元数;
//公共浮点四元数z;
//公共浮动四元数w;
公共字节状态;
公共短期状态2;
公共字节CRC;
}
}
SizeConst
表示固定长度数组或 要导入的字符串中的字符数(不是字节)

因此,您可能希望将其设置为3和4,而不是分别设置为12和16,那么此结构的大小将为38字节

SizeConst
表示固定长度数组或 要导入的字符串中的字符数(不是字节)


因此,您可能希望将其分别设置为3和4,而不是12和16-然后此结构的大小将为38字节。

运行此代码时会发生什么-是否存在异常?您是否有一些示例输入和预期输出?当您运行此代码时会发生什么-是否存在异常?您是否有一些示例输入和预期输出?