Skip to content

Instantly share code, notes, and snippets.

@musoftware
Last active January 26, 2020 20:15
Show Gist options
  • Save musoftware/a85e66190958066d20a68f18ad6916a7 to your computer and use it in GitHub Desktop.
Save musoftware/a85e66190958066d20a68f18ad6916a7 to your computer and use it in GitHub Desktop.
convert cusom base
<?php
function toBase($num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$r = $num % $b ;
$res = $base[$r];
$q = floor($num/$b);
while ($q) {
$r = $q % $b;
$q =floor($q/$b);
$res = $base[$r].$res;
}
return $res;
}
function to10( $num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$limit = strlen($num);
$res=strpos($base,$num[0]);
for($i=1;$i<$limit;$i++) {
$res = $b * $res + strpos($base,$num[$i]);
}
return $res;
}
$dec = '125377';
$t = toBase($dec);
echo $t . "\n";
echo to10($t);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment