Last active
October 14, 2018 09:25
-
-
Save kevinyan815/b5ed01e56c027467c1620776a4e6cba7 to your computer and use it in GitHub Desktop.
base62_convert.php
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
/** | |
* Convert a numeric string from base 10 to another base. | |
* | |
* @param $value decimal string | |
* @param int $b base , max is 62 | |
* @return string | |
*/ | |
function to_base($value, $b = 62) | |
{ | |
$base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$r = $value % $b; | |
$result = $base[$r]; | |
$q = floor($value / $b); | |
while ($q) | |
{ | |
$r = $q % $b; | |
$q = floor($q / $b); | |
$result = $base[$r].$result; | |
} | |
return $result; | |
} | |
/** | |
* Convert a string from a given base to base 10. | |
* | |
* @param string $value string from given base | |
* @param int $b base, max is 62 | |
* @return string | |
*/ | |
function to_base10($value, $b = 62) | |
{ | |
$base = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$limit = strlen($value); | |
$result = strpos($base, $value[0]); | |
for($i = 1; $i < $limit; $i++) | |
{ | |
$result = $b * $result + strpos($base, $value[$i]); | |
} | |
return $result; | |
} | |
/** | |
* Convert a 10 base numeric string to a 62 base string | |
* | |
* @param int $value | |
* @return string | |
*/ | |
function base62_encode($value) | |
{ | |
return to_base($value, 62); | |
} | |
/** | |
* Convert a string from base 62 to base 10 numeric string | |
* | |
* @param string $value | |
* @return int | |
*/ | |
function base62_decode($value) | |
{ | |
return to_base10($value, 62); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment