#!/usr/bin/perl use MIME::Base64; use Math::Base36 ':all'; my $value = 64000; print "$value MIME::Base64-> "; my $encoded = encode_base64($value); chomp($encoded); print $encoded; my $decoded = decode_base64($encoded); print " back-> $decoded\n"; print "$value Math::Base36-> "; $encoded = encode_base36($value); print $encoded; $decoded = decode_base36($encoded); print " back-> $decoded\n"; print "$value Math::Base62-> "; $encoded = Math::Base62::encode_base62($value); print $encoded; $decoded = Math::Base62::decode_base62($encoded); print " back-> $decoded\n"; package Math::Base62; use warnings; use strict; sub encode_base62 { return alphaID(shift); } sub decode_base62 { return alphaID(shift,1); } ## port of: https://raw.github.com/kvz/kvzlib/master/php/functions/alphaID.inc.php sub alphaID { my ($in, $to_num, $pad_up, $passKey) = @_; my $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; # if($passKey) { # Although this function's purpose is to just make the # ID short - and not so much secure, # with this patch by Simon Franz (http://blog.snaky.org/) # you can optionally supply a password to make it harder # to calculate the corresponding numeric ID # for(my $n = 0; $n 0) { # $out -= pow($base, $pad_up); # } # } $out = sprintf('%F', $out); $out = substr($out, 0, index($out, '.')); } else { # Digital number -->> alphabet letter code # if (is_numeric($pad_up)) { # $pad_up--; # if ($pad_up > 0) { # $in += pow($base, $pad_up); # } # } $out = ''; for(my $t = int( log($in)/log($base) ); $t >= 0; $t--) { my $bcp = $base**$t; my $a = int($in / $bcp) % $base; $out = $out . substr($index, $a, 1); $in = $in - ($a * $bcp); } $out = reverse($out); } return $out; }