我有我已經轉換爲原始字節的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?從'雙'結束:) –