Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Converting Hexadecimal to Decimal notation and vice-versa

by Anonymous Monk
on Oct 17, 2009 at 17:42 UTC ( [id://801773]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have a hexadecimal number as below
0x000102030405060708090a0b0c0d0e0f;

I was trying to do
#!/usr/bin/perl use strict; use warnings; my $x_dec = Math::BigInt->from_hex("0x000102030405060708090a0b0c0d0e0f +"); print $x,"\n";

But the above code does not work. I get an error message saying integer over flow.
Are there any other alternatives?
--Bella

Replies are listed 'Best First'.
Re: Converting Hexadecimal to Decimal notation and vice-versa
by Not_a_Number (Prior) on Oct 17, 2009 at 18:03 UTC

    It works for me (once I've fixed the errors in your posted code):

    use strict; use warnings; use Math::BigInt; # Missing from your posted code my $x_dec = Math::BigInt->from_hex("0x000102030405060708090a0b0c0d0e0f +"); print $x_dec, "\n"; # Not $x, as in your posted code # Output: 5233100606242806050955395731361295
      Hi, thanks.
      We had an older version of Math::BigInt and that is why it seems it could not locate method from_hex.
      How do I convert a huge integer to hexadecimal using Math::BigInt?
        How do I convert a huge integer to hexadecimal

        ->as_hex()

Re: Converting Hexadecimal to Decimal notation and vice-versa
by stonecolddevin (Parson) on Oct 17, 2009 at 21:51 UTC
      But how does this apply to the big integers of the OP?
      >perl -wMstrict -le "my $int = unpack('N', pack('H8', '102030405060708090a0b0c0d0e0f')); my $dec = sprintf('%d', $int); print $dec; " 270544960

        Specify a different template for sprintf?

        mtfnpy

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found