我有我已经转换为原始字节的UInt16s的二维数组 - 我想采取这些字节并将它们转换回原始的二维数组。我设法用2d阵列的双打做到这一点,但我无法弄清楚如何用UInt16做到这一点。将byte []转换为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语句不显示为原始二维数组相同的值。我对字节和UInt16编码非常陌生,所以我在这个过程中学到了大部分。另外,我知道我的代码的最后一块没有像原始数组一样将值放入一个2d数组中 - 现在我只是试图打印出值来查看它们是否与原始数据匹配。
'/ 8','* 8'-w hy 8而不是sizeof(UInt16)这是2?从'双'结束:) –