in reply to Syllable Generator

Hey, that reminds me of a cool little script I wrote. I had an assignment where I had to convert lots of numbers from decimal to binary, and each converstion had to be accompanied by tediously high amounts of working out. Being lazy, out came Perl and out came a nice script which printed out the working for each question. Woohoo! It was something like:

20 / 2 = 10 rem 0 
10 / 2 = 5 rem 0 
5 / 2 = 2 rem 1
2 / 2 = 1 rem 0
1 / 2 = 0 rem 1 

Therefore 20 in binary is 
10100

Replies are listed 'Best First'.
RE: RE: Syllable Generator
by chromatic (Archbishop) on Jun 05, 2000 at 20:41 UTC
    The functions pack and unpack can be of great help here, if you don't want to do a loop:
    my $number = "12345"; my $bin_number = unpack("B32", pack("N", $number)); print "$number in binary is $bin_number\n";
    Use a regex to get rid of leading zeroes.