Jump to content
  • Advertisement
Sign in to follow this  
Haytil

Host and Network Byte Order for Floats

This topic is 4403 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I asked this as a follow-up question to a topic in "For Beginners," but because it is more specific to network programming, I decided to briefly ask here instead: When sending floats over a TCP connection, assuming that I'm not interested in packing the floats into a size less than 4 bytes (as described in the multiplayer forum FAQ), would I have to worry about endianness issues? Or is host and network byte order not an issue for floats? If it is, how would I handle this issue? I cannot find an equivalent of the htonl()/ntohl() functions for floats: only for shorts and longs, i.e., 16-bit and 32-bit integers. Do I have to do it manually? (If so, how would I detect the endianness of the machine the program is running on?) -Gauvir_Mucca

Share this post


Link to post
Share on other sites
Advertisement
Endianness does matter for floats. Since a float is a 32-bit quantity, you can use htonl/ntohl to do the conversion, something like:
float a;
long convertedFloat = htonl(*reinterpret_cast<long *>(&a));

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!