If you want to know how conversion of numbers to a string works, try this:
$num = 1234567;
$result = "";
$base = 2;
do {
use integer;
my $digit = $num % $base;
substr($result, 0, 0) = $digit; # for any number < 10
$num /= $base;
} while $num;
print $result;
If you like fixed width output, add enough zeros to the front, afterwards:
substr($result, 0, 0) = "0" while length($result) < 32;
If you want to play with $base > 10, for example hexadecimal , try:
$num = 1234567;
$base = 16;
$result = "";
my @digit = (0 .. 9, 'A' .. 'Z');
do {
use integer;
my $digit = $num % $base;
substr($result, 0, 0) = $digit[$digit]; # for base up to 36
$num /= $base;
} while $num;
print $result;
Note that the do { ... } while $num; construct makes the loop execute at least once, so you get a proper result for zero. |