byte[,]和byte[][]如何转换为byte[]

「已注销」 2014-03-01 05:01:57
c/c++里的byte mydata[5][5],mydata[0]就是一个数组的首地址
那c#里一个二维数组如何转换为一维数组?
谢谢!
...全文
181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2014-03-02
  • 打赏
  • 举报
回复
如果是交错数组,可以用linq: byte[] result = data.SelecMany(x => x).ToArray();
tcmakebest 2014-03-02
  • 打赏
  • 举报
回复
这个写循环就可以解决了,先计算总长度,然后 new 一下,再复制,感觉没有什么技巧可用。
threenewbee 2014-03-02
  • 打赏
  • 举报
回复
byte[] result = new byte[data.GetLength(0) * data.GetLength(1)]; for (int i = 0; i < result.GetLength(0); i++) { result[i] = data[i / data.GetLength(1), i % data.GetLength(1)]; }
「已注销」 2014-03-02
  • 打赏
  • 举报
回复
谢谢各位!!!
emailtome 2014-03-01
  • 打赏
  • 举报
回复
public static IEnumerable<T> ToEnumerable<T>(this Array target) { foreach (var item in target) yield return (T)item; } test: int[,] aa = new int[2, 2] {{1, 2}, {1, 2}}; int[,,] aaa = new int[2,2,2] {{{1,2},{1,2}}, {{1,2},{1,2}}}; var enumerable = from i in aaa.ToEnumerable<int>() select i; foreach (var i in enumerable) { Console.WriteLine(i); } foreach (var i in aaa) { Console.WriteLine(i); } Console.Clear(); Console.WriteLine(aaa[1,0,0]);
JianDom 2014-03-01
  • 打赏
  • 举报
回复
List<byte> a; 二维数组循环添加到a中 byte[] = a.ToArray() 这个效率较差 二维数组很少用

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧