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?


In reply to Re: newbie: unpacking real numbers by jmcnamara
in thread newbie: unpacking real numbers by perchance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.