Last active
January 26, 2020 20:15
-
-
Save musoftware/a85e66190958066d20a68f18ad6916a7 to your computer and use it in GitHub Desktop.
convert cusom base
This file contains hidden or 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 | |
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