2

我有一个二维字节数组,想从一维数组中更新/复制每个数据数组。

var multi = new byte[5, 200];

var single0 = new byte[200]; // initialized to some data

var single1 = new byte[200]; // initialized to some data

var single2 = new byte[200]; // initialized to some data

var single3 = new byte[200]; // initialized to some data

var single4 = new byte[200]; // initialized to some data


Buffer.BlockCopy(single0, 0, multi, 0, single0.Count());

Buffer.BlockCopy(single1, 0, multi, 1, single1.Count());

Buffer.BlockCopy(single2, 0, multi, 2, single2.Count());

Buffer.BlockCopy(single3, 0, multi, 3, single3.Count());

Buffer.BlockCopy(single4, 0, multi, 4, single4.Count());

但这并没有按预期工作。只有第一行被更新。

任何帮助表示赞赏。谢谢。

4

1 回答 1

5

你的第四个论点是错误的。它是目标数组中的偏移量。该目标数组实际上是 5 个连续的 200 字节的“块”。所以你会想要:

int stride = single0.Length; // Or multi.GetLength(1)
Buffer.BlockCopy(single0, 0, multi, stride * 0, stride);
Buffer.BlockCopy(single1, 0, multi, stride * 1, stride);
Buffer.BlockCopy(single2, 0, multi, stride * 2, stride);
Buffer.BlockCopy(single3, 0, multi, stride * 3, stride);
Buffer.BlockCopy(single4, 0, multi, stride * 4, stride);
于 2013-02-18T11:51:31.343 回答