Drone
Drone

Reputation: 181

single byte array to 2d byte array in C#?

I have a byte array of

byte[] d = new byte[64];

now i want to convert it to a 2d byte array like ..

byte[,] data = new byte[8,8];

can any one help me on this

Upvotes: 3

Views: 4032

Answers (3)

Adil
Adil

Reputation: 148110

This could be one of method.

byte[] d = new byte[64];
byte[,] data = new byte[8,8];

int row = 0;
int column = 0;

for(i=0; i < d.Length; i++)
{
   row = i%8;
   column = i/8;
   data [row, column] = d[i];    
}

Upvotes: 6

Adriaan Stander
Adriaan Stander

Reputation: 166326

How about something like

byte[] d = new byte[64];

for (byte i = 0; i < d.Length; i++)
    d[i] = i;

byte[,] data = new byte[8, 8];

Enumerable.Range(0, 8).ToList().
    ForEach(i => Enumerable.Range(0, 8).ToList().
        ForEach(j => data[i, j] = d[i * 8 + j]));

Upvotes: 0

dtb
dtb

Reputation: 217233

You can use the Buffer.BlockCopy Method:

byte[] d = new byte[64];
byte[,] data = new byte[8,8];

Buffer.BlockCopy(d, 0, data, 0, 64);

Upvotes: 4

Related Questions