We help IT Professionals succeed at work.

How to convert network to host (vice versa) Byte order in doubles and floats? in C/C++

praocareer
praocareer asked
on

How to convert network to host (vice versa) Byte order in doubles and floats? in C/C++?

I need to convert both 32bit double and 64 bit
Comment
Watch Question

Sr Manager Software Development
Commented:
http://stackoverflow.com/questions/6084279/host-to-network-double

As explained in this article, the storage of double is NOT consistent across multiple computer architecture/compilers.
If you really want to send double value across platforms you can split the sign, exponent and fraction part into different integers and combine them at the destination. But you need to know how double value is handled at both the ends.
If you both application are following XDR format, then the above method is better and easy option to follow.

 For more information about XDR format look into http://en.wikipedia.org/wiki/Double_precision
Alternatively, you could convert the double/floats to text and send that.  This allows you a degree of flexibility over how the receiver and sender store the values.  This is a fairly common way of dealing with the transfer of real numbers between systems.