Created
July 23, 2015 18:35
-
-
Save aaronpk/90b65905b44393c2c0a0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Converts base 10 to base 60. | |
* http://tantek.pbworks.com/NewBase60 | |
* @param int $n | |
* @return string | |
*/ | |
function b10to60($n) | |
{ | |
$s = ""; | |
$m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz"; | |
if ($n==0) | |
return 0; | |
while ($n>0) | |
{ | |
$d = $n % 60; | |
$s = $m[$d] . $s; | |
$n = ($n-$d)/60; | |
} | |
return $s; | |
} | |
/** | |
* Converts base 60 to base 10, with error checking | |
* http://tantek.pbworks.com/NewBase60 | |
* @param string $s | |
* @return int | |
*/ | |
function b60to10($s) | |
{ | |
$n = 0; | |
for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s | |
{ | |
$c = ord($s[$i]); // put current ASCII of char into $c | |
if ($c>=48 && $c<=57) { $c=$c-48; } | |
else if ($c>=65 && $c<=72) { $c-=55; } | |
else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1 | |
else if ($c>=74 && $c<=78) { $c-=56; } | |
else if ($c==79) { $c=0; } // error correct typo capital O to 0 | |
else if ($c>=80 && $c<=90) { $c-=57; } | |
else if ($c==95) { $c=34; } // underscore | |
else if ($c>=97 && $c<=107) { $c-=62; } | |
else if ($c>=109 && $c<=122) { $c-=63; } | |
else { $c = 0; } // treat all other noise as 0 | |
$n = (60 * $n) + $c; | |
} | |
return $n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is a fork from a historical (and out of date) implementation of NewBase60. Please see https://github.com/tantek/cassis/blob/master/cassis.js#num_to_sxg for the latest JS/PHP code and feel free to fork from there.