10进制转62进制

10进制转62进制,把大小写字母充当占位符

            let tmp = template()
            let str = toSixTwo(100004)
            let num = toTen(str)
            console.log(str)  // qRp
            console.log(num) //  100004

            // 33-47=14 58-64=6 91-96=5 123-126=3 65-90=25 97-122=25
            // 10进制转成62进制
            function toSixTwo(num) {
                let rule = 61
                let a = num % rule
                let b = (num / rule) >> 0
                let str = tmp[a]
                let index = 0
                while (b>0){
                    index++
                    a = b%rule
                    b = (b / rule) >> 0
                    str = tmp[a] + str
                }
                return str
            }
            // 62转10进制
            function toTen(numStr){
                let rule = 61
                let arr = numStr.split('')
                let num = 0
                arr.reverse()
                arr.map((value,index)=>{
                    console.log(value)
                    num += (tmp.findIndex(val=>val===value) * Math.pow(rule,index))
                })
                return num
            }
            // 生成模板
            function template(){
                let arr = []
                let lower = 97
                let upper = 65
                let letterNum = 122-lower+1
                for(let i=0;i<62;i++){
                    if(i<10){
                        arr.push(i.toString())
                    }else if(i<10+letterNum){
                        arr.push(String.fromCharCode(lower))
                        lower++
                    }else if(i<10+letterNum+letterNum) {
                        arr.push(String.fromCharCode(upper))
                        upper++
                    }
                }
                return arr
            }
参考:
  • 二进制如何转十进制,十进制如何转二进制
  • JavaScript实现Base64编码转换

你可能感兴趣的:(10进制转62进制)