isync has asked for the wisdom of the Perl Monks concerning the following question:
#!/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/alp +haID.inc.php sub alphaID { my ($in, $to_num, $pad_up, $passKey) = @_; my $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQ +RSTUVWXYZ"; # 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<length($index); $n++) { # $i[$n] = substr($index,$n,1); # } # $passhash = hash('sha256',$passKey); # $passhash = (strlen($passhash) < strlen($index)) # ? hash('sha512',$passKey) # : $passhash; # for ($n=0; $n < strlen($index); $n++) { # $p[] = substr($passhash, $n ,1); # } # array_multisort($p, SORT_DESC, $i); # $index = implode($i); # } my $base = length($index); my $out; if($to_num) { # Digital number <<-- alphabet letter code $in = reverse($in); $out = 0; my $len = length($in) - 1; for(my $t = 0; $t <= $len; $t++) { my $bcpow = $base**($len - $t); $out = $out + index($index, substr($in, $t, 1)) * $bcpow +; } # if($pad_up =~ /\d+/) { # $pad_up--; # if ($pad_up > 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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: RFC: Math::Base62
by BrowserUk (Patriarch) on Oct 04, 2011 at 16:19 UTC | |
by isync (Hermit) on Oct 04, 2011 at 19:15 UTC |