Convert byte[,] to byte[]

后端 未结 4 586
后悔当初
后悔当初 2021-01-15 22:32

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

4条回答
  •  遥遥无期
    2021-01-15 23:26

    You can't get a managed byte[] array from a byte[,] without copying it out (not that I know of, anyway).

    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.
        }
    }
    

提交回复
热议问题