【发布时间】:2016-09-22 18:47:00
【问题描述】:
我有一个 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]);
}
打印语句显示的值与原始二维数组不同。我对字节和 UInt16 编码很陌生,所以我在这个过程中学习了大部分内容。
*另外,我知道我的代码的最后一块并没有像原始数组那样将值放入二维数组中——现在我只是想打印出这些值,看看它们是否与原始数据匹配。
【问题讨论】:
-
/ 8,* 8- 为什么是 8 而不是 sizeof(UInt16) 是 2?从double结转 :)
标签: c# arrays byte type-conversion