On Tue, 07 Oct 2008 11:30:01 -0700, chrisben
<ch******@discussions.microsoft.comwrote:
Hi,
Assuming I have a class/struct with only following types
float, int, string
what is the most efficient way to pack it to a single byte array before I
send it out. I tried to use Bitconverter.GetBytes, but not sure what is
the
best/quickest way to combine all bytes together.
One way to do it would be to take the byte arrays as they are converted
and write them to a MemoryStream instance.
However, if you're doing stream-oriented output, you might find the
BinaryWriter and BinaryReader (for the other end) more appropriate. You
could use the BinaryWriter to write to a MemoryStream instance which you
then convert to a byte array after all the data's been read and send those
bytes, or if you're dealing with some sort of stream in the first place,
just write directly to the stream with the BinaryWriter and skip the
MemoryStream altogether.
Pete