views:

10766

answers:

4

In the documentation of hardware that allows us to control it via UDP/IP, I found the following fragment:

In this communication protocol, DWORD is a 4 bytes data, WORD is a 2 bytes data, BYTE is a single byte data. The storage format is little endian, namely 4 bytes (32bits) data is stored as: d7-d0, d15-d8, d23-d16, d31-d24; double bytes (16bits) data is stored as: d7-d0 , d15-d8.

I am wondering how this translates to C#? Do I have to convert stuff before sending it over? For example, if I want to send over a 32 bit integer, or a 4 character string?

+2  A: 

Re little-endian, the short answer (to do I need to do anything) is "probably not, but it depends on your hardware". You can check with:

bool le = BitConverter.IsLittleEndian;

Depending on what this says, you might want to reverse portions of your buffers. Alternatively, Jon Skeet has specific-endian converters here (look for EndianBitConverter).

Note that itaniums (for example) are big-endian. Most Intels are little-endian.

Re the specific UDP/IP...?

Marc Gravell
... Is anyone using .net on itanium?
TokenMacGuy
hmm. What basic research I just did seems to suggest that Windows is always little-endian (Itaniums can support either endiannesses). So BitConverter.IsLittleEndian seems like it's *always* going to return true, unless you're running something wierld like Mono on a big-endian linux on itanium.See http://blogs.msdn.com/larryosterman/archive/2005/06/07/426334.aspx
piers7
One of the comments suggests xna on xbox may be big-endian; I haven't checked: http://blogs.msdn.com/larryosterman/archive/2005/06/07/426334.aspx#426591
Marc Gravell
+19  A: 

C# itself doesn't define the endianness. Whenever you convert to bytes, however, you're making a choice. The BitConverter class has an IsLittleEndian field to tell you how it will behave, but it doesn't give the choice. The same goes for BinaryReader/BinaryWriter.

My MiscUtil library has an EndianBitConverter class which allows you to define the endianness; there are similar equivalents for BinaryReader/Writer. No online usage guide I'm afraid, but they're trivial :)

(EndianBitConverter also has a piece of functionality which isn't present in the normal BitConverter, which is to do conversions in-place in a byte array.)

Jon Skeet
+3  A: 

You need to know about network byte order as well as CPU endian-ness.

Typically for TCP/UDP comms, you always convert data to network byte order using the 'http://msdn.microsoft.com/en-us/library/ms738557.aspx">htons' function (and ntohs, and their related functions).

Normally network order is big-endian, but in this case (for some reason!) the comms is little endian, so those functions are not very useful. This is important as you cannot assume the UDP comms they have implemented follow any other standards, it also makes life difficult if you have a big-endian architecture as you just can't wrap everything with htons as you should :-(

However, if you're coming from an intel x86 architecture, then you're already little-endian, so just send the data without conversion.

gbjbaanb
+9  A: 

You can also use

IPAddress.NetworkToHostOrder(...)

For short, int or long.

Jan Bannister
This works!!! Thanx! =D
Cipi