如何在 C# 中将多维数组转换为一维数组?
我有一个多维数组,其中包含二进制数据 [3,240]:
byte[,] bData = (byte[,])objTran; // bdata is binary data with [3,240]
这意味着它有 3 条记录,分别来自 0,239、1,239 和 2,239。 现在我正在编组此数据以复制到 TRANSACTIONLOGINFO 结构中:
GCHandle handle = GCHandle.Alloc(bData, GCHandleType.Pinned);
TRANSACTIONLOGINFO ObjTranInfo = (TRANSACTIONLOGINFO)Marshal.PtrToStructure(
handle.AddrOfPinnedObject(), typeof(TRANSACTIONLOGINFO));
handle.Free();
但是每次 ObjTranInfo
仅显示第一个数据详细信息。如何将多维数组转换为一维数组并传递给 GCHandle 来一一获取每个数据?
I have one multi-dimensional array which contains binary data as [3,240]:
byte[,] bData = (byte[,])objTran; // bdata is binary data with [3,240]
which means it has 3 records each from 0,239 and 1,239 and 2,239.
Now I am Marshalling this data to copy in TRANSACTIONLOGINFO structure:
GCHandle handle = GCHandle.Alloc(bData, GCHandleType.Pinned);
TRANSACTIONLOGINFO ObjTranInfo = (TRANSACTIONLOGINFO)Marshal.PtrToStructure(
handle.AddrOfPinnedObject(), typeof(TRANSACTIONLOGINFO));
handle.Free();
But every time ObjTranInfo
shows only the first data details. How can I convert the multi-dimensional array to a single-dimensional array and pass to GCHandle
to get each data one by one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建一个正确大小的一维字节数组(在您的例子中为 240),并使用
for
循环一次跨一个字节复制数据。对原始二维数组的每一行重复此操作。Create a single-dimensional byte array of the right size (in your case 240) and copy the data across a byte at a time using a
for
loop. Repeat this for every row of the original two-dimensional array.