Skip to main content
GameDev.net gamedev.net
🔒 Locked

C# :: Mixing Data Types

Started by Zipster Apr 24, 2004 at 3:41 PM 1 replies 250+ views
Original Post
Zipster
Zipster
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
Zipster
Zipster
That would work great! I assume I could also do:
string s = "\xFF\xFF\xFF\xFF" + "infostring";?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.