Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
in most examples of client / server communication a string is encoded to an array of bytes as a message, I would need something more powerful, because I need to send commands and parameters, and sometimes images.
What is the best thing to be encoded in a byte array that is not a string?
I do not want to split in order to understand what is the command and its parameters.
Posted

You could add a "command header" to the beginning of your byte array that is comprised of a known length, say 64 or 128 bytes or even larger (to give you room to grow). Then, you just plug in your command/param values into that area. As long as both the sending and receiving apps are aware of this header, they can process it.

At that point, you can make the header contain anything you want, including integers, strings (converted to hex bytes), DateTime objects, etc. As long as both apps know how to process it (I would use a separate assembly that both apps reference), you're gold.

Heck, you could even serialize an object to a byte array, add a couple of bytes at the beginning to describe it's length, and pre-pend THAT to your byte array.
 
Share this answer
 
v2
Comments
Morph King 12-Jun-11 8:07am    
thanks for the reply, so I have to add each parameter as the array of bytes?
instead serialize an instance of a class or an array or an enum is not correct?
#realJSOP 12-Jun-11 8:11am    
You can serialize an object if you want to. Or, you can create your header as a fixed-length array one byte at a time (as long as the final length is always the same (or your first byte (or two) describes the length.
Morph King 12-Jun-11 8:19am    
Thanks 5 stars
codesharper 13-Jun-11 1:51am    
good answer my 5
Albert Holguin 13-Jul-11 13:53pm    
In another words, make an API. My 5, but OP should prob understand that an array of bytes can represent anything.
An array of bytes is not an alternative to string, an array of bytes is an abstract serialized representation of every data possible. You cannot possibly transmit anything else, can you? :-)

The real alternative is transmitting binary data, which I would recommend if you need top performance. Needless to say, this data will be serialized to an array of bytes anyway, but the amount of bytes can be essentially reduced.

—SA
 
Share this answer
 
Comments
Albert Holguin 13-Jul-11 13:52pm    
Don't think that second paragraph makes too much sense, but the first does, a string is just bytes, so you can put anything into it. That's how application APIs are specified, with bytes and byte offsets. My 4.
Sergey Alexandrovich Kryukov 13-Jul-11 14:45pm    
Albert, a string is "just bytes" only in the same sense as any other data. If this is Unicode, serialization of the string produce variable amount of bytes, depending on characters in it. A character can take one to four bytes, even in UTF-16 (not very well known fact, but characters beyond BMP are expressed with surrogate pairs). Only with UTF-32 (pretty rarely used as it's impractical) a character gets exactly 4 byte. So, unless you're talking about ASCII/ANSI, a string is not "just bytes".
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900