首页 > 其他分享 >十进制转换为2~36的任意进制

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

时间:2023-01-29 14:25:10浏览次数:38  
标签:const 进制 number 36 base baseStr 十进制 stack

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

const baseConverter = (number = 12138, base = 2) => {
    const stack = []
    const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let baseStr = ''
    if(!(base >= 2 && base <= 36))return
    let rem = 0
    while(number > 0){
        rem = Math.floor(number % base)
        stack.push(rem)
        number = Math.floor(number / base)
    }
    while(stack.length){
        baseStr += digits[stack.pop()]
    }
    return baseStr
}

  

标签:const,进制,number,36,base,baseStr,十进制,stack
From: https://www.cnblogs.com/zhenjianyu/p/17072552.html

相关文章