Saturday, May 31, 2014

Converting Binary to Decimal

Script

#!/usr/bin/perl

print ("Enter a Binary number: ");
$bina_number = <STDIN>;
chop ($bina_number);

$decimal = oct("0b".$bina_number);

print ("The decimal equivalent of the Binary number $bina_number is:  ", $decimal);
print ("\n");


Execution

Enter a Binary number: 1011
The decimal equivalent of the Binary number 1011 is:  11



Enter a Binary number: 1111
The decimal equivalent of the Binary number 1111 is:  15



Enter a Binary number: 10111110
The decimal equivalent of the Binary number 10111110 is:  190



Enter a Binary number: 11111111
The decimal equivalent of the Binary number 11111111 is:  255


Enter a Binary number: 1111111111111111
The decimal equivalent of the Binary number 1111111111111111 is:  65535



Enter a Binary number: 11001011
The decimal equivalent of the Binary number 11001011 is:  203


No comments:

Post a Comment