develooper Front page | perl.beginners | Postings from July 2001

Re: How to convert hexadecimal number in decimal ?

Thread Previous
From:
merlyn
Date:
July 4, 2001 08:19
Subject:
Re: How to convert hexadecimal number in decimal ?
Message ID:
m1vgl8zpra.fsf@halfdome.holdit.com
>>>>> "Maxim" == Maxim Berlin <mak@rtsnet.ru> writes:

Maxim> unfortunatelly, i do not remember where i got this :(

It's also mislabeled.  Replace "dec" with "num" in every case.
It's just that Perl does "num2dec" and "dec2num" trivially. :)

Maxim> bin2dec:
 
Maxim>   $out = unpack("N", pack("B32", substr("0" x 32 . $in, -32))); 
 
Maxim> bin2hex:
 
Maxim>   $out = unpack("H8", pack("B32", substr("0" x 32 . $in, -32))); 
 
Maxim> bin2oct:
 
Maxim>   $out = sprintf "%o", unpack("N", pack("B32", substr("0" x 32 . $in, -32))); 
 
Maxim> dec2bin:
 
Maxim>   $out = unpack("B*", pack("N", $in));
 
Maxim> dec2hex:
 
Maxim>   $out = unpack("H8", pack("N", $in));
 
Maxim> dec2oct:
 
Maxim>   $out = sprintf "%o", $in;
 
Maxim> hex2bin:
 
Maxim>   $out = unpack("B32", pack("N", hex $in));
 
Maxim> hex2dec:
 
Maxim>   $out = hex $in;
 
Maxim> hex2oct:
 
Maxim>   $out = sprintf "%o", hex $in;
 
Maxim> oct2bin:
 
Maxim>   $out = unpack("B32", pack("N", oct $in));
 
Maxim> oct2dec:
 
Maxim>   $out = oct $in;
 
Maxim> oct2hex:
 
Maxim>   $out = unpack("H8", pack("N", oct $in));


-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About