十进制转换为2~36的任意进制

/**
* 十进制转换为2~36的任意进制
*/
const baseConverter = (number = 12138, base = 2) => {
    if(base < 2 || base > 36) return new Error('进制参数错误')
    const stack = []
    const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    while (number > 0) {
        const rem = number % base
        stack.push(rem)
        number = Math.floor(number / base)
    }
    let res = ''
    while (stack.length) {
        res += digits[stack.pop()]
    }
    return res
}

  

posted @ 2023-01-29 14:07  671_MrSix  阅读(83)  评论(0编辑  收藏  举报