How to convert fixed byte/char[100] to managed char[] in C#?

落爺英雄遲暮 提交于 2019-12-11 01:27:28

问题


What's the best way to convert a fixed byte or char[100] to a managed char[] in C#? I ended up having to use pointer arithmetic and I'm wondering if there is an easier way -- something like a memcpy or another way?

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

namespace StructTest
{

    [StructLayout(LayoutKind.Explicit)]
    unsafe struct OuterType
    {
        private const int BUFFER_SIZE = 100;

        [FieldOffset(0)]
        private int transactionType;

        [FieldOffset(0)]
        private fixed byte writeBuffer[BUFFER_SIZE];

        public int TransactionType
        {
            get { return transactionType; }
            set { transactionType = value; }
        }

        public char[] WriteBuffer
        {
            set
            {
                char[] newBuffer = value;

                fixed (byte* b = writeBuffer)
                {
                    byte* bptr = b;
                    for (int i = 0; i < newBuffer.Length; i++)
                    {
                         *bptr++ = (byte) newBuffer[i];
                    }
                }
            }

            get
            {
                char[] newBuffer = new char[BUFFER_SIZE];

                fixed (byte* b = writeBuffer)
                {
                    byte* bptr = b;
                    for (int i = 0; i < newBuffer.Length; i++)
                    {
                        newBuffer[i] = (char) *bptr++;
                    }
                }

                return newBuffer;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            OuterType t = new OuterType();
            t.WriteBuffer = "hello there".ToCharArray();
            System.Console.WriteLine(t.WriteBuffer);
        }
    }
}

回答1:


You can use Marshal.Copy for that. Notice it is also overloaded for byte[], which might be a more appropriate data type.




回答2:


I don't know of a better way to do the conversion on a fixed variable. However one way to make this simpler is to avoid the use of a fixed variable altogether. Instead use a normal C# array and mark it as a UnmanagedType.ByValArray

[FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, SizeConst = BUFFER_SIZE)]
private byte[] writeBuffer;

Then you can use a simple LINQ query to translate the data. Full solution below

[StructLayout(LayoutKind.Explicit)]
unsafe struct OuterType
{
    private const int BUFFER_SIZE = 100;

    [FieldOffset(0)]
    private int transactionType;

    [FieldOffset(0), MarshalAs(UnmanagedType.ByValArray, SizeConst = BUFFER_SIZE)]
    private byte[] writeBuffer;

    public int TransactionType
    {
        get { return transactionType; }
        set { transactionType = value; }
    }

    public char[] WriteBuffer
    {
        set { writeBuffer = value.Cast<byte>().ToArray(); }
        get { return writeBuffer.Cast<char>().ToArray(); }
    }
}


来源:https://stackoverflow.com/questions/1185269/how-to-convert-fixed-byte-char100-to-managed-char-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!