in reply to newbie: unpacking real numbers
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; # 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; }
#!/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; }
|
|---|