Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

long integer to hexadecimal conversion

by perl_fan (Novice)
on Oct 01, 2009 at 10:08 UTC ( #798585=perlquestion: print w/ replies, xml ) Need Help??
perl_fan has asked for the wisdom of the Perl Monks concerning the following question:

Hello Gurus, i have a long integer which i want to convert to hexadecimal. When i try to convert the integer to hexadecimal beyond certain digits, it gives the value as 'ffffff'
#!/usr/local/bin/perl $intval = 3143230627; $hexval = sprintf ("%x", $intval); printf "value of $intval : $hexval \n"; $intval = 123143230627; $hexval = sprintf ("%x", $intval); printf "value of $intval : $hexval \n";
The output
> ./t.pl value of 3143230627 : bb59e4a3 value of 123143230627 : ffffffff
any help will be appreciated. Note: would like to use inbuilt functions or modules, because any other module installation will be unacceptable to the project. cheers, M

Comment on long integer to hexadecimal conversion
Select or Download Code
Re: long integer to hexadecimal conversion
by Anonymous Monk on Oct 01, 2009 at 10:16 UTC
Re: long integer to hexadecimal conversion
by Bloodnok (Vicar) on Oct 01, 2009 at 10:16 UTC
    I don't know for sure (as I've not used it in anger), but I'd guess that using unpack might be the way to go.

    I've no doubt that the more learned members of the fraternity|(brother|sister)hood will enlighten us both...

    A user level that continues to overstate my experience :-))
      tried unpack same result :(
        The basic problem appears to be that you have a 32 bit machine and you need a bigger integer than can be represented in 32 bits.

        If you have 4 bits, the biggest unsigned number that you can represent is with all 4 bits "on", is, "1111", or 15 in decimal, 2**4-1=15, or 'F'. 0123456789ABCDEF is all 16 possibilities with 4 bits.

        The biggest unsigned 32 bit number is 2**32-1= 4,294,967,295.

        The normal convention for representing a "signed" number is what is called 2's complement arithmetic. If you have 4 bits and have say 0001, to get "-1", you complement (reverse) all bits and then add one. 1110+1=1111. Now we come to the interpretation of whether "1111" means "15" or "-1". Basically the most significant bit becomes the "sign bit". 0111 with 4 bits is the max positive number or 2**3-1=7. 1000 is the max negative number which weirdly enough is -8.

        So if you have 32 bits as signed 2's complement, you get "0111 1111 1111 1111 1111 1111 1111 1111" or 2,147,483,647 as the max positive number and -2,147,483,648 as the max negative number.

        I think a Perl float uses more than 32 bits for the "mantissa". I think it maybe as many as 53 bits. I'm not sure. I think the "safest" way is to use these big int modules. The 32 bit integer to think about is 2 billion. When you get to that size integer number, things can get more complicated.

        123,143,230,627 is a lot bigger than 2,147,483,647 and hence the problems.

Re: long integer to hexadecimal conversion
by Joost (Canon) on Oct 01, 2009 at 10:49 UTC
      I wanted to say the same thing, but I'm late, I guess:

      use bigint; my $n = 123456789123456; my ($h, $d); while ($n > 0) { $d = $n % 16; $n /= 16; $h .= sprintf "%x",$d; } print scalar reverse $h;
        couldn't have been easier than this. Thanks kikuchiyo, you saved my day

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-07-23 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (152 votes), past polls