• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Network byte order

## Recommended Posts

If i am transferring data between little endian and big endian computers, i need to call htons() etc. on my shorts and longs. I am assuming i would still need to do this even if i am converting everything to an array of bytes before sending it? If so, how do i get a correct byte order for float''s, double''s etc. that aren''t longs or shorts? Or do i just have to make sure that if i am going to use data other than only bytes shorts and longs, i will only run the program on little endian systems? Thanks

##### Share on other sites
Just do some casting magic:
float Float;long NetworkFloat;NetworkFloat = htonl( *((long *)(&Float)) );

You can do it with unions too (which might be cleaner).

##### Share on other sites
You shouldn''t be sending floats across the network in the first place. The bit layout of floats can vary a lot between different machines, so basically you can either scale your values and send them as integers (of course, that only works if your floats aren''t really _floating_ point), or you will have to deal with the per-CPU-type internal layout of floats. Good luck.

cu,
Prefect

##### Share on other sites
Thats ok, i have a friend who knows ASM! haha
Thanks for that, hopefully the majority of the programs will be run on x86 machines, if it''s neccessary to run it on something else we''ll cross that bridge when we come to it.
Thanks,
CYer, Blitz

• ### Forum Statistics

• Total Topics
628278
• Total Posts
2981785

• 10
• 11
• 17
• 13
• 9