in reply to converting big hex to dec

Update: Apparently I should point out that this code (obviously) has the same limitation as any code that uses Perl's NV (double) format numbers, it is limited to 53-bits of precision.

sub hex16dec{ my( $hi, $lo ) = split '(.{8}$)', $_[0]; return hex( $hi ) * 2**32 + hex( $lo ); } $value = '123456789ABCDEF'; printf "%.f\n", hex16dec $value; 81985529216486896

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Replies are listed 'Best First'.
Re^2: converting big hex to dec
by jmcnamara (Monsignor) on Sep 27, 2005 at 12:45 UTC

    Unfortunately that will exceed the 15 digit precision of a 64bit double. In fact, your answer doesn't match the result from the 64bit machine shown above, or this:
    > perl -Mbigint -le 'print 0x123456789ABCDEF' 81985529216486895

    --
    John.

      That's why I chose to show that particular value, to point out the limitation and make the OP think about what he is doing.

      If the OP's task is simply to convert ascii-hex to ascii-decimal, why is he (clumsily) restricting the scope of BigInt?

      The only reason for limiting it's scope that came to mind, is if he is doing math within the script that he doesn't want slowed by BigInt being involved, but if that is the case, then he should also realise that the moment he moves the 64-bit integer outside of the scope of BigInt, he faces the possibility of loss of precision when the values are greater than 53-bits.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

        That's why I chose to show that particular value, to point out the limitation and make the OP think about what he is doing.

        I knew that. I just chose to show a counter example to point out the limitations of answering a question with incorrect code and no commentary.

        --
        John.

Re^2: converting big hex to dec
by merlyn (Sage) on Sep 27, 2005 at 16:48 UTC
    sub hex16dec {
    That's misnamed. It's not returning a decimal string. It's returning a number without a base. It's Perl's number-to-string conversion that is doing the "decimalification" later.

    So a better name for that would be "from_hex_16" or something like that.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Agreed. doubleFromHex16() maybe?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.