将byte []转换为UInt16。

时间:2016-09-22 18:47:01

标签: c# arrays byte type-conversion

我有一个2d的UInt16数组,我已经转换为原始字节 - 我想把这些字节转换回原来的2D数组。我已经设法用2d数组的双打来做到这一点,但是我无法弄清楚如何用UInt16做到这一点。

这是我的代码:

UInt16[,] dataArray; 
//This array is populated with this data:
[4 6 2]
[0 2 0]
[1 3 4]

long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);

var bufferUInt16 = new byte[byteCountUInt16Array];
Buffer.BlockCopy(newUint16Array, 0, bufferUInt16, 0, bufferUInt16.Length);


//Here is where I try to convert the values and print them out to see if the values are still the same:

UInt16[] originalUInt16Values = new UInt16[bufferUInt16.Length / 8];
for (int i = 0; i < 5; i++)
{
    originalUInt16Values[i] = BitConverter.ToUInt16(bufferUInt16, i * 8);
    Console.WriteLine("Values: " + originalUInt16Values[i]);
}

print语句没有显示与原始2d数组相同的值。我对使用字节和UInt16进行编码非常陌生,所以我在这个过程中学习了大部分内容。

*另外,我知道我的代码的最后一块并不是像原始数组那样将值放入二维数组中 - 现在我只是试图打印出值来查看它们是否匹配原始数据。

3 个答案:

答案 0 :(得分:2)

该计划

    public static void Main(string[] args)
    {
        UInt16[,] dataArray = new ushort[,]{ {4,6,2}, {0,2,0}, {1,3,4}}; 
        //This array is populated with this data:

        long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);

        var byteBuffer = new byte[byteCountUInt16Array];
        Buffer.BlockCopy(dataArray, 0, byteBuffer, 0, byteBuffer.Length);

        for(int i=0; i < byteBuffer.Length; i++) {
            Console.WriteLine("byteBuf[{0}]= {1}", i, byteBuffer[i]);
        }


        Console.WriteLine("Byte buffer len: {0} data array len: {1}", byteBuffer.Length, dataArray.GetLength(0)* dataArray.GetLength(1));
        UInt16[] originalUInt16Values = new UInt16[byteBuffer.Length / 2];
        for (int i = 0; i < byteBuffer.Length; i+=2)
        {
            ushort _a = (ushort)( (byteBuffer[i]) | (byteBuffer[i+1]) << 8);
            originalUInt16Values[i/2] = _a;
            Console.WriteLine("Values: " + originalUInt16Values[i/2]);
        }
    }

输出

byteBuf[0]= 4
byteBuf[1]= 0
byteBuf[2]= 6
byteBuf[3]= 0
byteBuf[4]= 2
byteBuf[5]= 0
byteBuf[6]= 0
byteBuf[7]= 0
byteBuf[8]= 2
byteBuf[9]= 0
byteBuf[10]= 0
byteBuf[11]= 0
byteBuf[12]= 1
byteBuf[13]= 0
byteBuf[14]= 3
byteBuf[15]= 0
byteBuf[16]= 4
byteBuf[17]= 0
Byte buffer len: 18 data array len: 9
Values: 4
Values: 6
Values: 2
Values: 0
Values: 2
Values: 0
Values: 1
Values: 3
Values: 4

您看到ushort,又称UInt16存储在4 = 0x04 0x00的字节顺序中,这就是我选择转换公式的原因

            ushort _a = (ushort)( (byteBuffer[i]) | (byteBuffer[i+1]) << 8);

将获取索引byte的{​​{1}}并在i取下一个byte并将其左移一个字节(8位)的大小上升i+1的16位。用orhter的话来说,ushort,然后重复。此转换代码专用于您所在机器的字节顺序,因此不可移植。

此外,我修复了ushort _a = 0x[second byte] 0x[first byte]数组较大的错误,因为它与因子byteBuffer相乘。 8的大小是ushort的两倍,因此我们只需要数组长度中的因子2。

答案 1 :(得分:2)

如果您想要的只是投射UInt16 [,] - &gt; Byte,然后是Byte-&gt; UInt16,您可以执行另一个Block复制,这在运行时非常快,代码应该如下所示:< / p>

UInt16[,] dataArray = new UInt16[,] {
    {4, 6, 2},
    {0, 2, 0},
    {1, 3, 4}
};
for (int j = 0; j < 3; j++)
{
    for (int i = 0; i < 3; i++)
    {
        Console.WriteLine("Value[" + i + ", " + j + "] = " + dataArray[j,i]);
    }
}
long byteCountUInt16Array = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(UInt16);

var bufferUInt16 = new byte[byteCountUInt16Array];

Buffer.BlockCopy(dataArray, 0, bufferUInt16, 0, bufferUInt16.Length);

//Here is where I try to convert the values and print them out to see if the  values are still the same:

UInt16[] originalUInt16Values = new UInt16[bufferUInt16.Length / 2];
Buffer.BlockCopy(bufferUInt16, 0, originalUInt16Values, 0, BufferUInt16.Length);
for (int i = 0; i < 5; i++)
{
     //originalUInt16Values[i] = BitConverter.ToUInt16(bufferUInt16, i * 8);
     Console.WriteLine("Values---: " + originalUInt16Values[i]);
}

顺便说一下,你只将每个UInt16分成两个字节,所以你应该计算你的新大小除以2而不是8

答案 2 :(得分:0)

你的演员阵容让你应该能够暗中做事

var list = new List<byte> { 1, 2 , 
var uintList = new List<UInt16>();

//Cast in your select
uintList = list.Select(x => (UInt16)x).ToList();
相关问题