Does this look right for the value in your example?

$n = unpack 'N', pack 'B32', '01000010001111000000000000000000';; ( $s, $c, $e ) = ( $n & 0x8000_0000, (( $n >> 24 ) & 0x7f) - 64, (($n & 0x00ffffff) / 0xffffff) * 16 );; print $s, $c, $e;; 0 2 3.75000022351743 $num = ($s? -1 : 1 ) * $e * 10**$c;; print $num;;

375.000022351743

If so, then the following modification of your subroutine may be what you need:

sub parse_E ($) { my $data = shift; ## The data comes in as a 4-byte string so... $data = unpack 'N', $data; ## We need to treat it as an unsigned i +nteger ## in order to do bitwise math on it my ($sign, $characteristic, $fraction); $sign = ($data & 0x80000000) ? -1 : 1; $characteristic = (($data >> 24) & 0x7f) - 64; $fraction = (($data & 0x00ffffff) / 0xffffff) * 16; ## Not the fraction raised to the power of the characteristic ## my $num = $sign * $fraction ** $characteristic; ## But rather, the fraction * 10 to the power of the characteristic my $num = $sign * $fraction * 10 ** $characteristic; printf("DEBUG: parse_E(%32s)\n\tsign: %d charisteristic: %d ". "fraction: %f = %f\n", unpack ("B32", $data), $sign, $characteristic, $fraction, $num); printf("DEBUG: unpacked characteristic %s\n", unpack ("B7", ($data >> 24) & 0x7f)); printf("DEBUG: unpacked fraction %7s%s\n", " ", unpack ("B24", $data & 0x00ffffff)); return $num; }

Some inferences drawn from here, though it could definitely be more clearly stated.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Mysteries of unpack("a", ...) by BrowserUk
in thread Mysteries of unpack("a", ...) by pspinler

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.