Apart from the usual corncern about endianness, the main problem with packing/unpacking floating point numbers is that there is no universal representation of how they are stored. However, the majority of modern platforms use the IEEE specification.*
Therefore, if you know in advance that you are writing/reading a IEEE floating point number in a particular endianness you could use a scheme like the following to guarantee some level of portability:
#!/usr/bin/perl -w
use strict;
# Check if "pack" gives the required IEEE 64bit float
#
my $little_endian;
# Some test data
my $teststr = pack "d", 1.2345;
my @hexdata =(0x8D, 0x97, 0x6E, 0x12, 0x83, 0xC0, 0xF3, 0x3F);
my $number = pack "C8", @hexdata;
if ($number eq $teststr) {
# Little Endian
$little_endian = 1;
}
elsif ($number eq reverse($teststr)) {
# Big Endian
$little_endian = 0;
}
else {
# Give up, gracefully or otherwise.
die "Required floating point format not supported on this platform
+.";
}
# Now use this information to pack a double
#
my $double = 3.1415;
my $packed_double;
if ($little_endian) {
$packed_double = pack "d", $double;
}
else {
$packed_double = reverse pack "d", $double;
}
If you are sure of the IEEE issue you can can probably just determine the endianness using the Config module and pack or reverse pack as necessary:
#!/usr/bin/perl -w
use strict;
use Config;
my $double = 3.1415;
my $packed_double;
if ($Config{byteorder} == 1234) {
$packed_double = pack "d", $double;
}
else {
$packed_double = reverse pack "d", $double;
}
John.
--
* Does anyone have an example of modern hardware that doesn't support this format?
|