Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Extra Long HEX to Decimal Converion Issue on Select Platforms

by awohld (Hermit)
on Jul 14, 2012 at 21:34 UTC ( [id://981858]=perlquestion: print w/replies, xml ) Need Help??

awohld has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to fix where the CPAN Testers are failing the Data::MEID module.

Basically when the HEX number is so big it ends up becoming negative when converted to Decimal on some platforms. I tried using Math::BigInt, but the problem still remains.

For example, on some platforms, when I convert 0xA1000000 Decimal get -159383552009606898 instead of 270113177609606898.


Is there a way to reliably and quickly convert large HEX numbers to Decimal on all platforms?


The line of code that's converting the big HEX number to Decimal is on line 162 in MEID.pm and looks like:
my $manufacturer = sprintf("%010d", Math::BigInt->new("0x$1"));

Replies are listed 'Best First'.
Re: Extra Long HEX to Decimal Converion Issue on Select Platforms
by roboticus (Chancellor) on Jul 14, 2012 at 21:41 UTC

    awohid:

    Have you tried "%010u" instead of "%010d"?

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Extra Long HEX to Decimal Converion Issue on Select Platforms
by mbethke (Hermit) on Jul 15, 2012 at 02:48 UTC
    What roboticus said. Your problem is not in the conversion from hex text to a bit pattern but in the later interpretation of that pattern as a signed or unsigned int.
Re: Extra Long HEX to Decimal Converion Issue on Select Platforms
by Athanasius (Archbishop) on Jul 15, 2012 at 16:34 UTC

    To clarify:

    In the first place, the conversions given are incorrect. According to the Calculator app that comes with Windows Vista, A100 0000 (hexadecimal) is 2,701,131,776 (decimal). Conversely, 270,113,177,609,606,898 (decimal), which the OP was expecting, is actually 3BF A281 0092 96F2 (hexadecimal).

    Next, sprintf("%u", 0x3BFA281009296F2) gives the correct answer on a 64int perl build, but on a 32int build it results in:

    Integer overflow in hexadecimal number at ...

    Yet even on a 64int build, which handles the command without overflow, use warnings produces:

    Hexadecimal number > 0xffffffff non-portable at ...

    All of which is a good indication that the OP’s idea to use Math::BigInt was the correct one.

    But Math::BigInt is a class, so its ‘numbers’ are actually objects, which should be operated-on by methods. So:

    #! perl use strict; use warnings; use Math::BigInt; my $n = Math::BigInt->new('0x3BFA281009296F2'); print $n->as_hex(), ' = ', $n->bstr(), "\n";

    Output:

    0x3bfa281009296f2 = 270113177609606898

    which is portable and robust.

    Athanasius <°(((><contra mundum

      Okay, so what I ended up doing was:
      my $n = Math::BigInt->new('0x3BFA281009296F2'); my $manufacturer = sprintf("%010s", $n->bstr );
      I uploaded the revised module and hopefully this time it passes on all systems.

      Thanks for the help!

        Curiously, this works, too:

        my $n = Math::BigInt->new('0x3BFA281009296F2'); my $manufacturer = sprintf "%010s", $n;

        So the temp variable $n can be eliminated:

        my $manufacturer = sprintf "%010s", Math::BigInt->new('0x3BFA281009296F2');

        I didn't test this on multiple platforms. I'm only pointing it out because I noticed an allusion to it in the Math::BigInt documentation.

        Jim

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://981858]
Approved by Perlbotics
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-04-23 17:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found