感谢。
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;
}
答案 0 :(得分:3)
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"