Pergunta

Qual é a melhor maneira de converter um byte fixo ou char [100] para um char conseguiu [] em C #? Acabei por ter de usar a aritmética de ponteiro e eu estou querendo saber se existe uma maneira mais fácil? - algo como um memcpy ou outra maneira

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);
        }
    }
}
Foi útil?

Solução

Você pode usar Marshal.Copy para isso. Aviso também é sobrecarregado para byte [], que pode ser um tipo de dados mais apropriado.

Outras dicas

Eu não sei de uma maneira melhor de fazer a conversão de uma variável fixa. No entanto, uma maneira de fazer isso mais simples é evitar o uso de uma variável fixa completamente. Em vez disso use uma matriz normal C # e marcá-lo como um UnmanagedType.ByValArray

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

Em seguida, você pode usar uma consulta LINQ simples para traduzir os dados. solução completa abaixo

[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(); }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top