http://www.perlmonks.org?node_id=952861


in reply to Fast - Compact That String

my @ch = (' ', '0'..'9', 'A'..'Z'); my $base = @ch; my $num = 2_565_726_408; my $s = ''; while ($num > 0) { $s = $ch[$num % $base] . $s; $num = int($num / $base); } print $s; # ZZZZZZ

(for small numbers you could space-pad the string on the left, if you like)

And the reciprocal procedure (string to number) could be

my @val; $val[ ord($ch[$_]) ] = $_ for 0..$#ch; my $s = ... my $num = 0; my $m = 1; for (reverse split //, $s) { $num += $m * $val[ ord($_) ]; $m *= $base; } print $num;