How can I serialize doubles and floats in C?
I have the following code for serializing shorts, ints, and chars.
unsigned char * serialize_char(unsign
The portable way: use frexp to serialize (convert to integer mantissa and exponent) and ldexp to deserialize.
The simple way: assume in 2010 any machine you care about uses IEEE float, declare a union with a float element and a uint32_t element, and use your integer serialization code to serialize the float.
The binary-file-haters way: serialize everything as text, floats included. Use the "%a" printf format specifier to get a hex float, which is always expressed exactly (provided you don't limit the precision with something like "%.4a") and not subject to rounding errors. You can read these back with strtod or any of the scanf family of functions.