I have a structure in c# with two members:
public int commandID;
public string MsgData;
I need to turn both these into a single byte array which then gets sent to a C++ program that will unpack the bytes , it will grab the first `sizeof(int) bytes to get the commandID and then the rest of the MsgData will get used.
What is a good way to do this in c# ?
The following will just return a regular array of bytes, with the first four representing the command ID and the remainder representing the message data, ASCII-encoded and zero-terminated.
You can switch out
Encoding.UTF8for e.g.Encoding.ASCIIif you wish – as long as your C++ consumer can interpret the string on the other end.