Host and Network Byte Order for Floats

Started by
0 comments, last by bakery2k1 17 years, 9 months ago
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
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));

This topic is closed to new replies.

Advertisement