in reply to Unpacking Floating Point on architectures with different endians
Real numbers (floats and doubles) are in the native machine format only; due to the multiplicity of floating formats around, and the lack of a standard "network" rep resentation, no facility for interchange has been made. This means that packed floating point data written on one machine may not be readable on another - even if both use IEEE floating point arithmetic (as the endian-ness of the memory repre sentation is not part of the IEEE spec). See also the perlport manpage.
I don't really know much about the machine format of floating point numbers, but if the only problem is endianness, unpacking as characters, reversing them, putting them back together, and then interpreting them as native format might work. Something like this:
#!/usr/bin/perl my $f = "3.6"; my $floatpack = pack("d",$f); my $floatpack2 = pack("c*",reverse unpack("c*",$floatpack)); $f2 = unpack("d",$floatpack2); print "f2=$f2\n"; my $floatpack3 = pack("c*",reverse unpack("c*",$floatpack2)); $f3 = unpack("d",$floatpack3); print "f3=$f3\n";
|
|---|