in reply to acme::tools num2code function

It took me a while to understand what num2code does, so let me try to explain a bit better:

If you call num2code($number, $output_length, $chars), it converts $number to base length($chars) using the characters in $chars as symbol of the output alphabet. Then the result is padded to $output_length characters.

So if you call it as num2code($number, 10, 'AB'), it will produce a string of 10 characters consisiting of A's and B's, where A means 0 and B means 1.

Replies are listed 'Best First'.
Re^2: acme::tools num2code function
by cztmonk (Monk) on Jun 11, 2012 at 09:12 UTC

    Thanks, good explanation. I've learned something new today.

    $number=5; $code=num2code($number, 10, 'AB'), print $code ,"\n";

    it results in this output:

    AAAAAAABAB

    thanks again

      I've learned something new today

      Along the same line of thought, you may be interested in Math::BaseCnv

      #!/usr/bin/perl use warnings; use strict; use Math::BaseCnv; #fast # Convert 63 from base-10 (decimal) to base-2 (binary) # $binary_63 = cnv( 63, 10, 2 ); my $time = time; print "time -> $time\n"; my $time_128 = cnv( $time, 10, 128 ); print "time_128 -> $time_128\n"; #this will sometimes print hidden newlines my $time_10 = cnv( $time_128, 128, 10 ); print "time_10 -> $time_10\n"; my $time_64 = cnv( $time, 10, 64 ); print "time_64 -> $time_64\n"; my $time_b64 = cnv( $time, 10, 'b64' ); print "time_b64 -> $time_b64\n"; exit;

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
        Thanks for this hint.