Network byte order

Started by
2 comments, last by BlitzKrieg 21 years, 10 months ago
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
Advertisement
Just do some casting magic:
float Float;long NetworkFloat;NetworkFloat = htonl( *((long *)(&Float)) );

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

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

Return to the Shadows
Widelands - laid back, free software strategy
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

This topic is closed to new replies.

Advertisement