我想使用 PHP 转换以 36 为基数的数字。功能base_convert
不起作用,因为我想转换大数字:如果我再次将其从基数 36 转换为十进制,我将无法获得初始数字。
我尝试了多个网站上提供的某些功能,但从未得到相同的结果。此外,这两个网站(Javascript)给出了相同的结果:
- http://www.unitconversion.org/numbers/base-10-to-base-36-conversion.html http://www.unitconversion.org/numbers/base-10-to-base-36-conversion.html
- http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/decimal-to-base-36/ http://www.translatorscafe.com/cafe/units-converter/numbers/calculator/decimal-to-base-36/
例如1010701001118000000000000000
必须转换为3IZS0ZE1RQ68W8SSW4
.
以下是我尝试过的功能(但不起作用):
- http://www.php.net/manual/fr/function.base-convert.php#34510 http://www.php.net/manual/fr/function.base-convert.php#34510
- http://www.php.net/manual/fr/function.base-convert.php#106546 http://www.php.net/manual/fr/function.base-convert.php#106546
- http://www.tonymarston.net/php-mysql/showsource.php?file=converter.php http://www.tonymarston.net/php-mysql/showsource.php?file=converter.php
- http://www.geoffray.be/blog/php/convertir-des-nombres-en-base-62 http://www.geoffray.be/blog/php/convertir-des-nombres-en-base-62
这里有两个简单的函数,使用维基百科上找到的算法,同时使用 bcmath 来计算,即使对于非常大的数字也是如此:
function fromDecimalToBase($in, $to) {
$in = (string) $in;
$out = '';
for ($i = strlen($in) - 1; $i >= 0; $i--) {
$out = base_convert(bcmod($in, $to), 10, $to) . $out;
$in = bcdiv($in, $to);
}
return preg_replace('/^0+/', '', $out);
}
function fromBaseToDecimal($in, $from) {
$in = (string) $in;
$out = '';
for ($i = 0, $l = strlen($in); $i < $l; $i++) {
$x = base_convert(substr($in, $i, 1), $from, 10);
$out = bcadd(bcmul($out, $from), $x);
}
return preg_replace('/^0+/', '', $out);
}
但是,我得到3izs0ze1rq66tifrpc
对于您提供的号码 - 也许您的转换有误?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)