利用进制转换压缩数字函数分享_javascript技巧
程序员文章站
2024-01-05 08:51:04
...
复制代码 代码如下:
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();
推荐阅读
-
利用进制转换压缩数字函数分享_javascript技巧
-
5个javascript的数字格式化函数分享_javascript技巧
-
5个javascript的数字格式化函数分享_javascript技巧
-
JavaScript日期时间与时间戳的转换函数分享_javascript技巧
-
将数字转换成大写的人民币表达式的js函数_javascript技巧
-
php中base_convert()进制数字转换函数实例_php技巧
-
JavaScript日期时间与时间戳的转换函数分享_javascript技巧
-
将数字转换成大写的人民币表达式的js函数_javascript技巧
-
javascript parseInt() 函数的进制转换注意细节_javascript技巧
-
php中base_convert()进制数字转换函数实例_php技巧