【问题标题】:unity c# byte array to struct contain data arrayunity c# byte array to struct 包含数据数组
【发布时间】:2019-02-17 00:31:27
【问题描述】:

我想将字节数组(大小为 38)转换为这个结构。

当我将代码编辑为 公共字节[]数组轴; 这段代码运行得很顺利。

请帮忙...谢谢!

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

【问题讨论】:

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

标签: c# arrays unity3d struct


【解决方案1】:

SizeConst 表示定长数组的元素个数或 要导入的字符串中的字符数(不是字节数)。

所以您可能想将其分别设置为 3 和 4 而不是 12 和 16 - 那么这个结构的大小将是 38 字节。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-21
      • 2012-10-15
      • 1970-01-01
      • 2023-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      相关资源
      最近更新 更多