Last active
July 13, 2021 12:18
-
-
Save flxxyz/820749e3d9c66c1ba06c4e2596995ee3 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 | |
/** | |
* 自定义多进制 | |
* @param $str 自定义的多进制字符串 | |
* @param $num 十进制数字 | |
* @return string | |
* @author alexander_phper | |
* @link https://blog.csdn.net/alexander_phper/article/details/51363491 | |
*/ | |
function xbin($str, $num){ | |
$num = floatval($num); | |
$x = strlen($str); | |
$arr = str_split($str); | |
$digit = fmod($num, $x); | |
$xbin = isset($arr[$digit]) ? $arr[$digit] : null; | |
$pre_digit = floor($num / $x); | |
if($pre_digit >= 1){ | |
$pre_digit = xbin($str, $pre_digit); | |
return $pre_digit.$xbin; | |
} | |
return $xbin; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
递归调用的方法名没有修改过来,xbin => xdec