Re: Type-punning / casting problem
On Sep 17, 2:16 am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Phil Endecott wrote:
James Kanze wrote:
[..]
You prefer something which isn't portable, and which contains a
lot of undocumented dependencies on how the types are
represented, to something that is guaranteed to work everywhere?
I'm only interested in platforms with IEEE 754 floating point. I
think that my memcpy-then-htonl code will work on all such platforms,
won't it?
You could simply convert the float into its hex representation using
the %a format (IIRC it's the lossless one), then sscanf it back...
file << std::setprecision( 7 ) << value ;
is also guaranteed to be lossless (for IEEE float). For that
matter, he *is* allowed to cast the pointer to un unsigned
char*, and output that (as hex, or otherwise). However, if the
protocol says that floating point numbers are in IEEE float
format, with the sign on the top bit, then the exponent, and the
mantissa on the bottom bits, and output with the high byte
first, then outputting hex or whatever won't help. When
outputting a protocol, you absolutely must conform to the
requirements of the protocol. Obviously, a protocol is easier
to implement and debug if it is all text, but not all protocols
are all text.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34