Convert byte[,] to byte[]

后端 未结 4 584
后悔当初
后悔当初 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:10

    Well, the object itself isn't a byte[], even though it's got the data itself in one contiguous block of memory. Don't forget that an array knows its ranks and lengths etc as well.

    However, you can use Buffer.BlockCopy to copy the array quickly. For example:

    byte[,] source = new byte[5,5];
    byte[] dest = new byte[source.Length];
    Buffer.BlockCopy(source, 0, dest, 0, source.Length);
    

    Note that the final argument is the number of bytes, not the number of array elements.

    0 讨论(0)
  • 2021-01-15 23:18
    var myFlattenedByteArray = my2DByteArray.SelectMany(b=>b).ToArray();
    

    This will return each element of each subarray of the 2D byte array in subarray-element order: myFlattenedByteArray[1] == my2DByteArray[0][1]. There are probably more performant or efficient solutions (especially the slupring of the Enumerable produced by SelectMany into its own Array) but it's a one-liner.

    0 讨论(0)
  • 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.
        }
    }
    
    0 讨论(0)
  • 2021-01-15 23:27

    To go from a 2D index to 1D index:

    int i = y * width + x;
    

    And back:

    int x = i % width;
    int y = i / width;
    

    Then the simplest is to just iterate over your array, copying the values one by one.

    0 讨论(0)
提交回复
热议问题