#!/usr/bin/perl -w use strict; my @alphanum = (0 .. 9, "a" .. "z", "A" .. "Z"); my $divisor = scalar @alphanum; #my $enc = &enc(1_000); #my $dec = &dec($enc); #print "$enc $dec\n"; for (my $x=0; $x<10_000; $x++) { print "$x -> " . &enc($x) . "\n"; } exit 0; sub dec { my $num = shift; # strip leading 0's $num =~ s/$0+//g; my ($y, $result) = (0, 0); foreach (split(//, reverse($num))) { my $found = 0; foreach my $item (@alphanum) { if ($item eq $_) { last; } $found++; } my $temp = $found * ($divisor ** $y); $result += $temp; $y++; } return $result; } sub reduce { return (int($_[0] / $divisor), $_[0] % $divisor); } sub enc { my $num = shift; my $end = ""; my ($a, $b) = reduce($num); $end = $alphanum[$b] . $end; until ($a < $divisor) { ($a, $b) = reduce($a); $end = $alphanum[$b] . $end; } $end = $alphanum[$a] . $end unless $a == 0; return $end; }