how to convert a short array to a byte array?
-
Hello gurus, I'd like to know how to convert an array of shorts (or what ever) to an array of bytes to send on a socket? And vice versa... Can anybody show me a code snipet please? Best regards. Fred.
There is no spoon.
Take a look at the MSDN topic of the
Array.ConvertAll
method.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook
-
Hello gurus, I'd like to know how to convert an array of shorts (or what ever) to an array of bytes to send on a socket? And vice versa... Can anybody show me a code snipet please? Best regards. Fred.
There is no spoon.
Sounds like
Buffer.BlockCopy()
is what you need. Eachshort
value will be split in half to become 2byte
values in the byte array.//'values' is the name of the array of short values
byte[] buffer=new byte[values.Length*2];
Buffer.BlockCopy(values,0,buffer,0,buffer.Length);
//'buffer' now holds the short values as bytesC# / DHTML / VG.net / MyXaml expert currently looking for work![^]