views:

249

answers:

3

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;
}
+8  A: 

$decimal/2; isn't affecting $decimal

You probably want $decimal /= 2; or if you want to be cool, then $decimal >>= 1;

But really, really, you probably just want:

printf "%b\n", $decimal;

David M
what does /= mean and what does printf do? Im sorry, im kind of new to perl
David
`/=` is division-assignment, in other words `$decimal /= 2` is equivalent to `$decimal = $decimal / 2`. `printf` is a formatted print function. If you really so new to programming that these aren't familiar to you, I would strongly suggest buying a book on Perl to get started with, rather than relying solely on asking questions to the Internet.
Tyler McHenry
'$decimal /= 2;' is shorthand for '$decimal = $decimal / 2;'A line like `$decimal/2;` doesn't do anything. It's exactly like having a line like `42;`printf means "print formatted". The '%b' means that the argument will be converted to binary when it's being output.
David M
also, i ran the input that you gave me and it converted the number properly, however, it kept printing the answer to the screen infinitely. Obviously there is something wrong with the logic of my while loop, but i do not see what it is.
David
@David If you used the `printf` line, then there is no need for a loop at all. The `printf` line does the complete job of converting a number to its binary representation and then printing it. Do try to find an introductory text on Perl. You're not going to get very far by copy-pasting other peoples' code that you don't really understand.
Tyler McHenry
oh yeah duh sorry. /= is like ++ or --. sorry i wasn't paying attention. And i appreciate what you are saying, however, i just joined this site a couple days ago. I have been programming in different languages a lot recently and I just had a few questions that i couldnt work out.
David
another advantage of using `printf` or `sprintf` is that they are `C` routines and will be orders of magnitude faster than any multi-step Perl solution
Eric Strom
+5  A: 

There are a few methods to convert from decimal to binary listed in perlfaq4 (How do I convert between numeric representations/bases/radixes?).

sprintf is a good choice.

toolic
A: 

I have these aliases in my .bash_profile for quick conversions on the command line:

alias d2h="perl -e 'printf qq|%X\n|, int( shift )'"
alias d2o="perl -e 'printf qq|%o\n|, int( shift )'"
alias d2b="perl -e 'printf qq|%b\n|, int( shift )'"
alias h2d="perl -e 'printf qq|%d\n|, hex( shift )'"
alias h2o="perl -e 'printf qq|%o\n|, hex( shift )'"
alias h2b="perl -e 'printf qq|%b\n|, hex( shift )'"
alias o2h="perl -e 'printf qq|%X\n|, oct( shift )'"
alias o2d="perl -e 'printf qq|%d\n|, oct( shift )'"
alias o2b="perl -e 'printf qq|%b\n|, oct( shift )'"
brian d foy