十进制转化为其他进制

1
2
3
4
5
6
7
8
9
10
11
12
function binary(num, base = 2) {
const stack = []
const digits = '0123456789ABCDEF'
while (num > base - 1) {
stack.push(digits[num % base])
num = ~~(num / 2)
}
stack.push(digits[num])
return stack.reverse().join('')
}

console.log(binary(10)) // '1010'