首页 > web前端 > js教程 > 利用进制转换压缩数字函数分享_javascript技巧

利用进制转换压缩数字函数分享_javascript技巧

WBOY
发布: 2016-05-16 17:05:40
原创
1268 人浏览过

复制代码 代码如下:

function zipNum(num, radix){
    if(!zipNum.zip){
        zipNum.zip = function(inputNum){
            if(inputNum > 35){//用大写字母表示36-61
                return String.fromCharCode('A'.charCodeAt(0) + inputNum % 36);
            } else if(inputNum > 9){//用小写字母表示10-35
                return String.fromCharCode('a'.charCodeAt(0) + inputNum % 10);
            } else {
                return inputNum;
            }
        }
    }
    var quotient = num, remainder, result;
    if(radix > 36 && radix         result = [];
        do {
            remainder = quotient % radix;
            quotient = parseInt(quotient / radix);
            result.push(zipNum.zip(remainder));
        }while(quotient > radix);
        remainder = quotient % radix;
        if(remainder !== 0){
            result.push(zipNum.zip(remainder));
        }
        result = result.reverse().join('');
    } else if(radix > 1 && radix         result = num.toString(radix);
    } else {
        result = num;
        window.console && console.warn('radix argument must be between 2 and 62')
    }
    return result;
}

function testSortNum(){
    assertEquals(zipNum(61, 62), 'Z');
    assertEquals(zipNum(62 * 2 - 1, 62), '1Z');
    assertEquals(zipNum(62 * 3 - 1, 62), '2Z');
    assertEquals(zipNum(1361182624991, 62), 'dXN5fJB');
}
testSortNum();
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板