Convert Binary Numbers to Decimal and vice-versa

Published on February 07, 2015
Python Program to Swap Two Numbers
Python Program to Swap Two Numbers

Convert Binary to Decimal and vice-versa

To convert a binary number to decimal base, use unpack and pack.

my $binnum = '1101'; # binary number 
my $decnum = unpack("N", pack("B32", substr('0'x32.$binnum, -32)));
print "$binnum = $decnum n";

To convert a decimal number to binary, use unpack.

my $decnum = 25; # decimal number
my $binnum = unpack("B32", pack("N", $decnum)); # in binary
$binnum =~ s!^0+(?=d)!!;
print "$decnum = $binnum n";
Table of Contents

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on February 07, 2015