-
-
Save hahasong/0793097f7c131ec893e6e48ccf9af6c8 to your computer and use it in GitHub Desktop.
PHP 10进制和36进制转换
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
| if (!function_exists('base36_encode')) { | |
| /** | |
| * 十进制数转换成三十六进制数 | |
| * @param (int)$num : 十进制数 | |
| * return (string) :三十六进制数 | |
| */ | |
| function base36_encode($num) | |
| { | |
| $num = intval($num); | |
| if ($num < 0) | |
| { | |
| return false; | |
| } | |
| $charArr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); | |
| $char = ''; | |
| do | |
| { | |
| $key = bcmod($num , 36); | |
| $char= $charArr[$key] . $char; | |
| $num = bcdiv(($num - $key),36, 0); | |
| } | |
| while ($num > 0); | |
| return $char; | |
| } | |
| } | |
| if (!function_exists('base36_decode')) { | |
| /** | |
| * 三十六进制数转换成十进制数 | |
| * @param (string)$char :三十六进制数 | |
| * return (int) :十进制数 | |
| */ | |
| function base36_decode($char){ | |
| $charArr = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'); | |
| $len = strlen($char); | |
| $sum = 0; | |
| for($i=0; $i<$len; ++$i) | |
| { | |
| $index = array_search( $char[$i], $charArr); | |
| $sum = bcadd($sum, bcmul($index, bcpow(36, $len-$i-1, 0))); | |
| } | |
| return $sum; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment