1
我试图将值的二维数组转换为一个字节[]并回到原始的二维数组。当运行我的代码我得到这个错误:复制字节时出错 - 偏移量和长度超出范围。
An unhandled exception of type 'System.ArgumentException' occurred in TestProject.exe.
Additional information: Offset and length were out of bounds
for the array or count is greater than the number of elements from the
index to the end of the source collection.
这里是我的代码:
byte[,] dataArray = new byte[,] {
{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 byteCountArray = dataArray.GetLength(0) * dataArray.GetLength(1) * sizeof(byte);
var bufferByte = new byte[byteCountArray];
Buffer.BlockCopy(dataArray, 0, bufferByte, 0, bufferByte.Length);
//Here is where I try to convert the values and print them out to see if the values are still the same:
byte[] originalByteValues = new byte[bufferByte.Length/2];
Buffer.BlockCopy(bufferByte, 0, originalByteValues, 0, bufferByte.Length);
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Values---: " + originalByteValues[i]);
}
在Buffer.BlockCopy行出现的错误:
Buffer.BlockCopy(bufferByte, 0, originalByteValues, 0, bufferByte.Length);
我是新来的编程/转换字节,所以任何帮助表示赞赏。
哇 - 新手的错误在我的部分。谢谢。 – Roka545