Converting 2 dimensional array to Single dimensional in C#?
Solution 1
You can use the Buffer.BlockCopy Method:
byte[,] bData = (byte[,])objTransLog;
byte[] baData = new byte[bData.Length];
Buffer.BlockCopy(bData, 0, baData, 0, bData.Length);
Example:
byte[,] bData = new byte[4, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 },
{ 10, 11, 12 }
};
byte[] baData = new byte[bData.Length];
Buffer.BlockCopy(bData, 0, baData, 0, bData.Length);
// baData == { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }
Solution 2
Simplest method
int iSize = Marshal.SizeOf(stTransactionLogInfo); //stTransactionLogInfo is a structure
byte[,] bData = (byte[,])objTransLog; //objTransLog is 2 dimensionl array from device
byte[] baData = bData.Cast<byte>().ToArray();
Solution 3
bData.Cast<byte>()
will convert the multi-dimensional array to a single dimension.
This will do boxing, unboxing so isn't the most performant way, but is certainly the simplest and safest.
Solution 4
easy to understend and conver to a different language.
// Create 2D array (20 rows x 20 columns)
int row = 20;
int column = 20;
int [,] array2D = new int[row, column];
// paste into array2D by 20 elements
int x = 0; // row
int y = 0; // column
for (int i = 0; i < my1DArray.Length; ++i)
{
my2DArray[x, y] = my1DArray[i];
y++;
if (y == column)
{
y = 0; // reset column
x++; // next row
}
}
Solution 5
If 2-D array's column size is dynamic, below code is usable:
public static T[] Convert2DArrayTo1D<T>(T[][] array2D)
{
List<T> lst = new List<T>();
foreach(T[] a in array2D)
{
lst.AddRange(a);
}
return lst.ToArray();
}
Bokambo
Updated on July 09, 2022Comments
-
Bokambo almost 2 years
I am converting 2dimensional array to Single dimensional in C#. I receive the 2 dimensional array from device (C++) and then I convert it to 1 dimensional in C#. Here is my code:
int iSize = Marshal.SizeOf(stTransactionLogInfo); //stTransactionLogInfo is a structure byte[,] bData = (byte[,])objTransLog; //objTransLog is 2 dimensionl array from device byte[] baData = new byte[iSize]; for (int i = 0; i < bData.GetLength(0); i++) { for (int j = 0; j < iSize; j++) { baData[j] = bData[i, j]; } }
I get the desired result from above code, but the problem is it is not the standard way of implementation. I want to know how it can be done in a standard way. May be doing Marshalling , I am not sure. Thanks in advance.
-
Bokambo over 12 yearsThis is not giving me the desired result.Buffer.BlockCopy will be inside for loop ?
-
Feidex over 12 yearsI've added an example. A
for
loop is not needed. -
Bokambo over 12 yearsFine.Now the Problem is It will copy all the data at once. But I want data in blocks of My Structure size (suppose size is 270) because i am using this binary data to form My Structure.Structure consists of several fields that are getting filled with data and at the Last i am showing this data in a grid,Row by Row.
-
Feidex over 12 yearsSo why aren't you using a
stTransactionLogInfo[]
instead of abyte[,]
? -
MPaul over 7 yearsWhat is the advantage of using the BlockCopy method as opposed to @Rakin 's answer?
bData.Cast<byte>().ToArray();
-
Nick Polyak about 3 yearsThe example works because it is a byte array. If you want some other types, e.g. integer arrays to work also, you need to get the byte length of the array int byteLength = Buffer.ByteLength(baData); and then pass it to the Buffer.BlockCopy as the last argument. The last argument should be the length in bytes not the length of the array.
-
derHugo about 2 yearsAdding to @NickPolyak ' comment: In general this only works for known base data types with a fix byte size. It will fail e.g. for
string
or any other dynamic sized type