Original Post
I''m trying to send a mixture of data to a server that includes character sequences and raw bytes. For instance, I need to send the character ''1'', following by the byte value 0, following by the character sequence (ie. non-terminated) "0.0.0.0:0", followed by the byte value 0 again, followed by some more mixtures of character sequences and raw bytes.
The issue is being able to do this as elegantly as possible. C# however is quite type-safe, so things are a bit more tricky then they usually would be in C++. Right now I have something that looks like this:
string data = "1" + (char)255 + "0.0.0.0:0" + (char)0; socket.SendTo( Encoding.Default.GetBytes(data), endpoint);And that works pretty well, for that specific instance. However, another message involves me sending four consecutive bytes of 255, following by a character sequence. What I have right now is this:
string data = "" + (char)255 + "" + (char)255 + "" + (char)255 + "" + (char)255 + "infostring";Which is a bastardized way of exploiting the overloaded operators to make sure it treats the 255''s as actual byte values and injects them into the string. Without the empty strings in between, it actually adds up all the 255''s (to 1020), converts them to a string, and then concatenates the string with "infostring", so I get "1020infostring", which is wrong. I''m wondering if there is a more general, consistent way of accomplishing what I want to do without having to resort to these brutal hacks. If this were C++ I''d just edit a character buffer directly with pointers, but without resorting to
unsafe C# makes things a bit more difficult