One solution I've seen elsewhere turned the float into an int by multiplying by a set power of ten. The precision was set by the spec, so they knew they only need 4 decimal places. Then they packed the int in network order (that's the N in your second example). When they unpacked it, they divided by the same power of ten.

If you want this to be really fast, you can cook up something simple in C (or even Inline::C) to do the same thing. Since you are using pack(), your Perl and C implementations will probably look the same so once you figure it out in Perl, it should be easy to translate to C.

There was one time I had to do this for several gigs of data, and I actually pulled out the assembly language to do it. The target was the Motorola 68k family, which had several nice instructions to flip these things around (flip the words, then flip the bytes in the words). That was a huge win even over the C code. I only got away with that because I knew exactly which microprocessors we had to use for the problem (ah, the joys of custom built hardware!)

Good luck!

--
brian d foy <bdfoy@cpan.org>

In reply to Re: Stumped by a pack/unpack problem by brian_d_foy
in thread Stumped by a pack/unpack problem by crenz

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.