4

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# ?

3
  • 1
    Is the C++ method that needs to receive this structure already defined? Could you show its signature in this case? Commented Oct 2, 2012 at 11:44
  • 1
    Use BinaryWriter together with Utf8Encoding. Commented Oct 2, 2012 at 11:48
  • no the byte array will be sent on a named pipe. So on the other end the cpp method will first crack open the commandID to route it to a handler function and then the remaining string ( the MsgData) will also get de serialized if you will by the cpp code . the string follow a simple Name1=Value1;Name2=Value2 format. If the handler code encounters an NV Pair it does not understand it ignores it So there is no signature exactly Commented Oct 2, 2012 at 11:49

3 Answers 3

4

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.

static byte[] GetCommandBytes(Command c)
{
    var command = BitConverter.GetBytes(c.commandID);
    var data = Encoding.UTF8.GetBytes(c.MsgData);
    var both = command.Concat(data).Concat(new byte[1]).ToArray();
    return both;
}

You can switch out Encoding.UTF8 for e.g. Encoding.ASCII if you wish - as long as your C++ consumer can interpret the string on the other end.

Sign up to request clarification or add additional context in comments.

3 Comments

ASCII encoding may be a mistake in terms of representing arbitrary string c.MsgData - maybe UTF-8 should be preferred?
I clearly don't enough about ascii vs UTF-8 here , is there some reason UTF-8 would be more portable in any way?On the .cpp side how would i turn the UTF encoded string into something useful like an STL string
UTF-8 can encode every character, but some characters may take more than one byte. ASCII can only encode the ASCII character set, but each character is exactly one byte. If you don't put any non-ASCII characters in your MsgData it shouldn't make a difference.
1

This directly goes to a byte array.

public byte[] ToByteArray(int commandID, string MsgData)
{
    byte[] result = new byte[4 + MsgData.Length];

    result[0] = (byte)(commandID & 0xFF);
    result[1] = (byte)(commandID >> 8 & 0xFF);
    result[2] = (byte)(commandID >> 16 & 0xFF);
    result[3] = (byte)(commandID >> 24 & 0xFF);
    Encoding.ASCII.GetBytes(MsgData.ToArray(), 0, MsgData.Length, result, 4);

    return result;
}

1 Comment

This doesn't provide the result it ought to. A byte has 8 bits, therefore you have to mask with FF respectively.
0

This will get you the byte[] that you want. One thing to note here is I didn't use a serializer because you wanted a very raw string and there are no serializers (that I know of) that can serialize it quite like you want OOB. However, it's such a simple serialization this makes more sense.

var bytes = Encoding.UTF8.GetBytes(string.Format("commandID={0};MsgData={1}", o.commandID, o.MsgData));

Finally, if you had more properties that are unknown to me you could use reflection.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.