gpt4 book ai didi

c# - byte[] array to struct with variable length 数组

转载 作者:可可西里 更新时间:2023-11-01 08:44:58 45 4
gpt4 key购买 nike

我正在从套接字接收一个字节数组,字节的结构只是一个固定宽度字符串的大字符数组。在某些情况下,最后一个字段是动态的(而不是固定长度),我正在尝试将字节编码为结构。我读过可变长度的 char 数组需要是 IntPtr,但我还没有弄清楚如何用剩余的字节对其进行编码。我还在一些文章中读到我可能需要第二个结构,但仍然无法弄清楚如何正确地编码它。

这是一个这样的 site

在结构中处理可变长度字符数组的正确方法是什么?

结构:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Header
{
#region private member fields

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f2;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f3;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f4;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f5;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f6;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
private char[] _f7;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
private char[] _f8;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
private char[] _f9;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
private char[] _f10;

// how would this get filled with a char[] array from the byte array?
public IntPtr VariableLengthData;

#endregion
}

函数:

public static Header FromArray(byte[] array)
{
IntPtr buff = IntPtr.Zero;

try
{
int objsize = Marshal.SizeOf(typeof(Header));
buff = Marshal.AllocHGlobal(objsize);
Marshal.Copy(array, 0, buff, objsize);
var result = (Header)Marshal.PtrToStructure(buff, typeof(HostHeader));

// the variable length data needs to be filled in somehow
// but also note that an extra 4 bytes was added to the size
// of the struct with the IntPtr
if(objsize < array.Length)
{
Marshal.Copy(array, array.Length - objsize, result.VariableLengthData, array.Length - objsize);
}

return result;
}
finally
{
if (buff != IntPtr.Zero)
{
Marshal.FreeHGlobal(buff);
buff = IntPtr.Zero;
}
}
}

这行得通 - 但现在 Marshal.SizeOf(headerObj) 说它比我尝试将它转换回 byte[] 数组时实际要小。除此之外,这个解决方案还有什么问题吗?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Header
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] Field1;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] Field2;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public char[] Field3;

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public char[] Field4;
}

public static Header DeserializeHeader(byte[] data)
{
int objsize = Marshal.SizeOf(typeof(Header));
IntPtr buff = Marshal.AllocHGlobal(objsize);
Marshal.Copy(data, 0, buff, objsize);
var header = (Header)Marshal.PtrToStructure(buff, typeof(Header));
Marshal.FreeHGlobal(buff);

// resize Field4 to hold all the remaining bytes
if(objsize < data.Length)
{
header.Field4 = Encoding.ASCII.GetChars(data, objsize - header.Field4.Length, data.Length - objsize - header.Field4.Length);
}
return header;
}

最佳答案

只是不要太努力让您的 C# 声明与数据包格式完全匹配。你会喜欢这个:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public class Header {
//....
}

public class Packet {
public Header Header;
public byte[] VariableLengthData;
}

现在变得简单了。您可以一口气编码 header ,只需复制存在的任何额外字节:

static unsafe Packet FromArray(byte[] data) {
var hdrsize = Marshal.SizeOf(typeof(Header));
if (data.Length < hdrsize) throw new ArgumentException();
Packet result = new Packet();
// Marshal the header
fixed (byte* pdata = &data[0]) {
result.Header = (Header)Marshal.PtrToStructure(new IntPtr(pdata), typeof(Header));
}
// Copy the rest
var varsize = data.Length - hdrsize;
result.VariableLengthData = new byte[varsize];
Array.Copy(data, hdrsize, result.VariableLengthData, 0, varsize);
return result;
}

并根据需要修改 Packet 类,您可能想要添加一堆属性来处理 char[]。

关于c# - byte[] array to struct with variable length 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28571098/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com