in reply to HEX to floating point

You can change the endianess of a whole string of binary data using scalar reverse, so long as you remember to reverse the template and results:

$raw = pack 'H*', '00000034000031b10191403b8811bb1366e4';; print for reverse unpack 'dCCVV', reverse $raw;; 52 12721 1 145 27.53152055

If you're working with large volumes of binary structures, doing it this way can save quite a lot of time.


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."

Replies are listed 'Best First'.
Re^2: HEX to floating point
by Spooky (Beadle) on Jun 16, 2008 at 14:47 UTC
    This is what I'd like to be able to do but when I entered your code into a file with the PERL load module I didn't get anything like your output - I did get a huge floating point number! ..any suggestions - I am new at PERL as if you didn't already guess... Here's my file: #!/fs/COTS/gnu/bin/AIX/perl $raw = pack 'H*", '00000034000031b110191403b8811bb1366e4'; print for reverse unpack 'dCCVV', reverse $raw ; ..when I run this (simply type "test.pl") I get : 1331232565922520241.709119561536

      First, you have a typo in your hex string: '00000034000031b110191403b8811bb1366e4' (the highlighted 1).  Second, if you run this on AIX (as I figure from the presence of "AIX" in the shebang path), you most likely don't want the reverse stuff anyway, as AIX typically runs on big-endian hardware, which already matches the binary format of your numbers...

      In other words, on AIX, this should work (actually it does — tried it):

      my $raw = pack "H*", '00000034000031b10191403b8811bb1366e4'; print "$_\n" for unpack 'NNCCd', $raw;
        Thanks so much - it work's great! One other question: what would be the best way to assign each value to a variable name? Again, thanks for your help - I'm definitely sold on PERL even though I'm a novice.
      I entered your code into a file with the PERL load module

      Hm. I'm afraid I have no idea what that means? What is the "Perl load module"?

      Perhaps you could cut & paste the terminal session to show what you did?


      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.
        This is what I'd like to be able to do but when I entered your code into a file with the PERL load module I didn't get anything like your output - I did get a huge floating point number! ..any suggestions - I am new at PERL as if you didn't already guess... Here's my file: #!/fs/COTS/gnu/bin/AIX/perl $raw = pack 'H*", '00000034000031b110191403b8811bb1366e4'; print for reverse unpack 'dCCVV', reverse $raw ; ..when I run this (simply type "test.pl") I get : 1331232565922520241.709119561536