[英]How to convert C# Struct to Byte Array
I have a structure which I want to send to a TCP client throught TCP protocol so I want to assign or copy this struct data to byte array: 我有一个要通过TCP协议发送到TCP客户端的结构,所以我想将此结构数据分配或复制到字节数组:
struct StartReadXML
{
public int CmdID;//3
public char[] CmdName;//ReadXML
public char[] Description;//Other data
};
here am assigning data to struct data members as below : 这里是将数据分配给结构数据成员,如下所示:
StartReadXML startXML=new StartReadXML();
startXML.CmdID = 3;
startXML.CmdName = "sreedhar".ToCharArray();
startXML.Description = "Kumar".ToCharArray();
Now, I want it to be assigned to a byte array. 现在,我希望将其分配给字节数组。 Which am doing using marshalling as below: 哪个正在使用编组,如下所示:
int sizestartXML = Marshal.SizeOf(startXML);//Get size of struct data
byte[] startXML_buf = new byte[sizestartXML];//byte array & its size
IntPtr ptr = Marshal.AllocHGlobal(sizestartXML);//pointer to byte array
Marshal.StructureToPtr(startXML, ptr, true);
Marshal.Copy(ptr, startXML_buf, 0, sizestartXML);
Marshal.FreeHGlobal(ptr);
//Sending struct data packet
stm.Write(startXML_buf, 0, startXML_buf.Length);//Modified
But, it fails at Structuretoptr conversion method. 但是,它在Structuretoptr转换方法中失败。 Please help in transferring the struct data as bytes for which am using above steps. 请帮助使用以上步骤将结构数据转换为字节。 Thanks in advance Smile | 在此先感谢微笑| :) !! :) !!
You cannot call StructureToPtr
on arrays of variable size. 您不能在可变大小的数组上调用StructureToPtr
。
What this boils down to is that unless you know the size of CmdName
and declare it - if it would be for example, 20 chars in size, like so: 归结为,除非您知道CmdName
的大小并声明它-举例来说,它的大小为20个字符,如下所示:
public fixed char[] CmdName[20];
You will be greeted with an exception from the Marshal saying that your structure is either non-blittable or no meaningful size can be obtained. 警官会给您一个例外,他说您的结构是不可膨胀的,或者无法获得有意义的尺寸。
This is a requirement the CLR imposes, and you can not work around. 这是CLR强制执行的要求,您无法解决。
An alternative method would be to use the Convert
class or a serializer to convert the members of your struct manually, but unless you know the size of those arrays up front, you won't be able to use StructureToPtr
- the same goes for the string
type, as I'm assuming that's what your char array will contain. 另一种方法是使用Convert
类或序列化程序手动转换结构的成员,但是除非您事先知道这些数组的大小,否则将无法使用StructureToPtr
string
类型,因为我假设这就是您的char数组将包含的内容。
Consider using a MemoryStream
and writing values to the stream, and sending the contents of the stream using stream.ToArray()
instead. 考虑使用MemoryStream
并将值写入流,然后使用stream.ToArray()
发送流的内容。
Considering that you can't simply convert to binary a struct, use for example: 考虑到您不能简单地将一个结构转换为二进制,请使用例如:
class StartReadXML
{
public int CmdID;//3
public string CmdName;//ReadXML
public string Description;//Other data
}
Then: 然后:
var srx = new StartReadXML();
srx.CmdID = 3;
srx.CmdName = "sreedhar";
srx.Description = "Kumar";
// Example of how to Write to byte[] buffer
byte[] buffer;
using (var ms = new MemoryStream())
{
using (var bw = new BinaryWriter(ms, Encoding.UTF8))
{
bw.Write(srx.CmdID);
bw.Write(srx.CmdName);
bw.Write(srx.Description);
}
buffer = ms.ToArray();
}
// Example of how to Read from byte[] buffer
var srx2 = new StartReadXML();
using (var ms = new MemoryStream(buffer))
{
using (var br = new BinaryReader(ms, Encoding.UTF8))
{
srx2.CmdID = br.ReadInt32();
srx2.CmdName = br.ReadString();
srx2.Description = br.ReadString();
}
}
Note that here I'm working with a "variable length" packet: the length of CmdName
and Description
aren't fixed (and BinaryWriter
/ BinaryReader
handle this by prepending the length of the string). 请注意,这里我使用的是“可变长度”数据包: CmdName
和Description
的长度不是固定的(并且BinaryWriter
/ BinaryReader
通过在字符串前加上长度来处理)。
The opposite is when you have a packet of fixed length, with fixed-length strings. 相反,当您有一个固定长度的数据包和固定长度的字符串时。 That requires a totally different handling. 这需要完全不同的处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.