ushort to byte[]

D

Dirk Reske

Hello,

I have:
ushort num = 0x0017;

how do I convert this to a byte array?
when I try BitConverter.GetBytes(num); I get {17,00}

I think I have to get {00,17}...
 
N

Nicholas Paldino [.NET/C# MVP]

Dirk,

If you have to get 17, 00, you can call the GetBytes method and then
pass the resulting array to the static Reverse method on the Array class.
This will flip the array around (in-place).

Are you working with sending something to a unix machine (or getting it
from a unix machine)? This is the only reason I can think of to use little
endian.

Hope this helps.
 
D

Dirk Reske

I don't know, on which os the server is running!
but this is for network communication.

some lines from protocol:
this is the packet header:
 
N

Nicholas Paldino [.NET/C# MVP]

Dirk,

You don't really need to know the server it is running on, just the
protocol you are sending it (what the byte order is in the protocol).

You know how to sort the bytes, does it work when you do that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top