转换字节[,]为byte [] [英] Convert byte[,] to byte[]
问题描述
有谁知道的扁平化的二维数组(非锯齿状)在C#中的一维和回来的有效途径。我知道在后端C#必须守住它作为一维数组我只想让后端1D阵列上的手柄,如果在所有可能的。
Does anyone know of an efficient way to flatten a 2d array (non-jagged) in C# to a 1d and back again. I know in the back end C# must hold onto it as a 1d array I would just like to get a handle on the back end 1d array if at all possible.
为什么我想这样做的原因是因为我希望能够在管理code把它作为一个2D,有时我想牵手而过是一维非托管的dll进口code(数字图像处理优化的汇编是一个很好的例子)。
The reason why I would like to do this is because I would like to be able to in managed code have it as a 2d, at times I would like to hand it off as a 1d to unmanaged dll imported code (optimized assembly in digital image processing is a good example).
推荐答案
您不能从字节的托管
不复制它(不,我知道的,反正)。字节[]
阵列[,]
You can't get a managed byte[]
array from a byte[,]
without copying it out (not that I know of, anyway).
如果您熟悉不安全
code可以解决一个字节*
在阵列上和我认为,应该工作:
If you're comfortable with unsafe
code you can fix a byte*
on the array and I believe that should work:
fixed (byte* ptr = array)
{
for (int i = 0; i < N; ++i)
{
byte b = ptr[i];
// Do whatever you need.
}
}
这篇关于转换字节[,]为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!