How do I convert decimal numbers to binary in Perl?

Posted by David on Stack Overflow See other posts from Stack Overflow or by David
Published on 2010-05-12T17:37:45Z Indexed on 2010/05/16 0:10 UTC
Read the original article Hit count: 639

Filed under:
|
|
|
|

I am trying to make a program that converts decimal numbers or text into binary numbers in perl. The program asks for user input of a character or string , and then prints out the result to the console. How do I do this? My code I have been working on is below, but i cannot seem to fix it.

print "Enter a number to convert: ";
chomp($decimal = <STDIN>);
print "\nConverting $number to binary...\n";
$remainder = $decimal%2;
while($decimal > 0)
{
    $decimal/2;
    print $remainder;
}

© Stack Overflow or respective owner

Related posts about perl

Related posts about binary