in reply to Re: Why does it return 0?
in thread Why does it return 0?

Thank you Ovid, I was actually going to use this but I have to actually do it manually.

Replies are listed 'Best First'.
Re: Re: Re: Why does it return 0?
by bart (Canon) on Feb 05, 2004 at 12:03 UTC
    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.