十进制转成其它进制

1.原理
2.循环的结束条件
3.超过十进制后的特殊处理

function baseConverter(decNumber, base){
        var remStack = [],
            rem,
            baseString ='',
            digits = '0123456789ABCDEF';
        
        while(decNumber > 0){
            rem = decNumber % base;
            remStack.push(rem);
            decNumber = Math.floor(decNumber / base);
        }

        while(remStack.length){
            baseString += digits[remStack.pop()]
        }
        return baseString;
    }   

console.log(baseConverter(100345, 2));
console.log(baseConverter(100345, 8));
console.log(baseConverter(100345, 16));

你可能感兴趣的:(十进制转成其它进制)