Personally I think $float = unpack 'f', pack 'L', unpack 'N', $data; is a better solution. It is conforming to the standard, ie, it acknowledges that the input data is in network long format. It is more portable, although you assume that float is 32 bits long. Consider the packing and unpacking process -

packing float as network long: +-----+ +-----------+ +------------+ |float|--->|native long|--->|network long| +-----+ +-----------+ +------------+ and unpacking network long back to float +------------+ +-----------+ +-----+ |network long|--->|native long|--->|float| +------------+ +-----------+ +-----+
where perl takes care of translation between native long (big or little endian) and network long (always big endian), without you having to worry explicitly what is the underlying architecture.

The $float = unpack "f", pack "N", unpack "V", $data; and the $num = unpack "f", reverse $packed_num; solutions both assume that you are on a little endian machine, making you dependent to the underlying architecture.

Update: I have written the following test program to test this solution-
use strict; use IO::File; savefloat(); # <--- this is run from Sun E10000 readfloat(); # <--- this is run from Windows XP sub savefloat { my $original_float = 1234.5678; my $network_long = pack 'N', unpack 'L', pack 'f', $original_float; my $f = new IO::File "NetworkLong.txt", "w"; print $f $network_long, "\n"; } sub readfloat { my $f = new IO::File "NetworkLong.txt", "r"; chomp(my $network_long = <$f>); my $float = unpack 'f', pack 'L', unpack 'N', $network_long; printf "%0.4f\n", $float; }
Two separate tests were conducted -

Test 1
On SUN: float->network_long->disk On WindowsXP: disk->network_long->float
Test 2
On WindowsXP: float->network_long->disk On SUN: disk->network_long->float
I got the same result in both test 1 and test 2. This proves that the particular packing method worked cross-platform on both Sun and WindowsXP, which are big endian and little endian platforms respectively.


In reply to Re: Network IEEE 754 to Native Floats: which pack/unpack ops better? by Roger
in thread Network IEEE 754 to Native Floats: which pack/unpack ops better? by shenme

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.