简体   繁体   English

单字节数组到C#中的2d字节数组?

[英]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 .. 现在我想将其转换为2D字节数组,如..

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

can any one help me on this 谁可以帮我这个事

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];    
}

You can use the Buffer.BlockCopy Method : 您可以使用Buffer.BlockCopy方法

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

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

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]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM