Domanda

Qual è il modo migliore per convertire un byte fissa o char [100] a un char gestito [] in C #? Ho finito per dover utilizzare l'aritmetica dei puntatori e mi chiedo se c'è un modo più semplice? - qualcosa come un memcpy o in un altro modo

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);
        }
    }
}
È stato utile?

Soluzione

È possibile utilizzare Marshal.Copy per questo. Si noti inoltre è sovraccarico per byte [], che potrebbe essere un tipo di dati più appropriato.

Altri suggerimenti

Non conosco un modo migliore per fare la conversione su una variabile fissa. Tuttavia un modo per fare questo semplice è quello di evitare l'uso di una variabile fissa del tutto. Utilizzare invece un normale C # array e segnare come un UnmanagedType.ByValArray

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

Quindi è possibile utilizzare una semplice query LINQ per tradurre i dati. soluzione completa sotto

[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(); }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top