tags:

views:

179

answers:

2

I'm working on a networking application in C#, sending a lot of plain numbers across the network. I discovered the IPAddress.HostToNetworkOrder and IPAddress.NetworkToHostOrder methods, which are very useful, but they left me with a few questions:

  1. I know I need to encode and decode integers, what about unsigned ones? I think yes, so at the moment I'm doing it by casting a pointer to the unsigned int into a pointer to an int, and then doing a network conversion for the int (since there is no method overload that takes unsigned ints)

    public static UInt64 HostToNetworkOrder(UInt64 i)
    {
        Int64 a = *((Int64*)&i);
        a = IPAddress.HostToNetworkOrder(a);
        return *((UInt64*)&a);
    }
    
    
    public static UInt64 NetworkToHostOrder(UInt64 a)
    {
        Int64 i = *((Int64*)&a);
        i = IPAddress.HostToNetworkOrder(i);
        return *((UInt64*)&i);
    }
    

    2. What about floating point numbers (single and double). I think no, however If I do need to should I do a similar method to the unsigned ints and cast a single pointer into a int pointer and convert like so?

EDIT:: Jons answer doesn't answer the second half of the question (it doesn't really answer the first either!), I would appreciate someone answering part 2

+1  A: 

I suspect you'd find it easier to use my EndianBinaryReader and EndianBinaryWriter in MiscUtil - then you can decide the endianness yourself. Alternatively, for individual values, you can use EndianBitConverter.

Jon Skeet
Well, I'll have a look at how that works and probably write my own implementation. The original questions still stand though, do I need to encode singles and uints?
Martin
Martin, byte order is indepenent of sign, so yes.
Steven Sudit
I guessed uints need to be encoded, I assume you're implying yes for single/double too?
Martin
I almost never use fp so I wouldn't know firsthand. However, it may well be that fp values are in a standard layout that's independent of byte order, so perhaps not.
Steven Sudit
Aye, that's the problem, I don't know and can't find any decent information on it :/
Martin
Unless Jon pipes up, I'd suggest doing a little experiment, rather than counting on reference material.
Steven Sudit
http://en.wikipedia.org/wiki/Endianness#Floating-point_and_endianness
Steven Sudit
I believe it *does* depend on the endianness of the machine, and I can't remember offhand what EndianBitConverter does... but as Steven suggests, it's probably best to experiment.
Jon Skeet
The wikipedia link seems to suggest that floating point numbers are indeed endian specific, I guess I'd better go and find myself a little endian test machine and play with this.
Martin
A: 

You'd better read several RFC documents to see how different TCP/IP protocols (application level, for example, HTTP/FTP/SNMP and so on).

This is generally speaking, a protocol specific question (both your questions), as your packet must encapsulate the integers or floating point number in a protocol defined format.

For SNMP, this is a conversion that changing an integer/float number to a few bytes and changing it back. ASN.1 is used.

http://en.wikipedia.org/wiki/Abstract%5FSyntax%5FNotation%5FOne

Lex Li
This is for a udp system, so the protocol is largely self defined. Basically I send an array of bytes to the other end, using some arbitrary networking system. Then I have to decode those bytes.
Martin
The question is mostly if endianness effects floats in the same way it effects integers
Martin