This article will list few examples on how to convert between binary and decimal numbers with Perl.
Binary to Decimal
First let’s show a basing example on how to convert from binary to decimal:
#!/usr/bin/perl $decimal_number = 0b1000; print $decimal_number;
Execution:
# ./convert.pl 8
Here is an another alternative method. In this case we will convert binary number 1000
to decimal:
#!/usr/bin/perl $binary_number=1010; $decimal_number = oct("0b".$binary_number); print $decimal_number;
Execution:
# ./convert.pl 10
Decimal to Binary
Below is an example on how to convert a decimal number to binary in this case the perl script will convert a decimal number 16
:
#!/usr/bin/perl $decimal_number=16; $binary_number= sprintf ("%b",$decimal_number); print $binary_number;
Execution:
# ./convert.pl 10000