gpt4 book ai didi

php - 是否有用于 PHP 的 base 高于 36 的 base 转换器?

转载 作者:行者123 更新时间:2023-11-30 13:29:56 25 4
gpt4 key购买 nike

php 只有 36 个 base 转换器,我需要更多,我找到了代码但是它在 js 中,谁能帮我把它翻译成 php?

谢谢。

    function baseConverter (number,ob,nb) {
// Created 1997 by Brian Risk. http://brianrisk.com
//number = number.toUpperCase();
var list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var dec = 0;
if(number=="0" || number=="" || number==0){
number="0";
}else{
for (var i = 0; i <= number.length; i++) {
dec += (list.indexOf(number.charAt(i))) * (Math.pow(ob , (number.length - i - 1)));
}
number = "";
var magnitude = Math.floor((Math.log(dec))/(Math.log(nb)));
for (var i = magnitude; i >= 0; i--) {
var amount = Math.floor(dec/Math.pow(nb,i));
number = number + list.charAt(amount);
dec -= amount*(Math.pow(nb,i));
}
}
return number;
}

最佳答案

 function baseConverter ($number,$ob,$nb) {
$list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
$dec = 0;
$number = (string)$number;
if(empty($number)){
$number="0";
}
else
{
$number_len = strlen($number);
for ($i=0; $i<$number_len; $i++)
{
$dec += strpos($list, $number[$i])*(pow($ob, ($number_len-$i-1)));
}
$number = '';
$magnitude = floor(log($dec)/log($nb));
for($i=$magnitude; $i>=0; $i--)
{
$amount = floor($dec/pow($nb, $i));
$number = $number.$list[$amount];
$dec-= $amount*(pow($nb, $i));
}
}
return $number;
}

var_dump( baseConverter("789", 10, 45));

will return string(2) "HO"

关于php - 是否有用于 PHP 的 base 高于 36 的 base 转换器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7122417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com