# 任意进制转换

/**
 * 其他进制转10进制
 * @param {Number} num 
 * @param {Number} n 
 */
function otherToTen(num, n) {
    let sum = 0;
    let a = 'a'.charCodeAt()
    let zero = '0'.charCodeAt()
    for (let i = num.length - 1; i >= 0; i--) {
        let char = num.slice(i, i + 1)
        let az = /[a-z]/
        let t = 0
        if (az.test(char)) {
            t = char.charCodeAt() - a + 10
        } else {
            t = char.charCodeAt() - zero
        }
        if (t >= n) {
            throw new Error('error num')
        }
        sum += ~~(t * Math.pow(n, num.length - 1 - i))
    }
    return sum
}
/**
 * 十进制转其它进制
 * @param {Number} num 
 * @param {Number} n 
 */
function tenToOther(num, n) {
    let zero = '0'.charCodeAt()
    let a = 'a'.charCodeAt()
    let res = ''
    while (num) {
        let t = num % n
        if (t >= 0 && t < 10) {
            res = String.fromCharCode(t + zero) + res;
        } else {
            t -= 10
            res = String.fromCharCode(t + a) + res;
        }
        num /= n
        num = ~~num
    }
    return res
}

console.log(otherToTen('1001011', 2)); // 75
console.log(tenToOther('9999', 16)); // 270f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50