如何在 C# 中将 int[,] 转换为 byte[]
如何在 C# 中将 int[,] 转换为 byte[]? 有些代码将受到赞赏
编辑:
我需要一个函数来执行以下操作:
byte[] FuncName (int[,] Input)
How to convert an int[,] to byte[] in C#?
Some code will be appreciated
EDIT:
I need a function to perform the following:
byte[] FuncName (int[,] Input)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您的问题中的细节很少,我只能猜测您要做什么...假设您想将 2D 整数数组“展平”为 1D 字节数组,您可以这样做:
请注意对
Cast
的调用:这是因为多维数组实现了IEnumerable
,但没有实现IEnumerable
Since there is very little detail in your question, I can only guess what you're trying to do... Assuming you want to "flatten" a 2D array of ints into a 1D array of bytes, you can do something like that :
Note the call to
Cast
: that's because multidimensional arrays implementIEnumerable
but notIEnumerable<T>
您似乎写错了类型,但您可能正在寻找以下内容:
It seem that you are writing the types wrong, but here is what you might be looking for:
这是假设您正在尝试序列化的一个实现;不过,不知道这是否是您想要的;它为尺寸添加前缀,然后每个单元格使用基本编码:
Here's an implementation that assumes you are attempting serialization; no idea if this is what you want, though; it prefixes the dimensions, then each cell using basic encoding:
BitConverter 将基本类型转换为字节数组:
您似乎希望将整数的二维数组转换为字节。将 BitConverter 与必要的循环结构(例如 foreach)以及您想要组合数组维度的任何逻辑相结合。
The BitConverter converts primitive types to byte arrays:
You appear to want a 2 dimensional array of ints to be converted to bytes. Combine the BitConverter with the requisite loop construct (e.g foreach) and whatever logic you want to combine the array dimensions.