转换
效果如下:
//var str="AZ";
//for(var i=0;i<str.length;i++)trace(str.charCodeAt(i));
//charCode:0-9:(48-57);A-Z:(65-90);a-z:(97-122);
//将10进制数N转换成d进制数(以0-9,A-Z,a-z字符表示),d为2-62
function jh_10toD(N, d) {
N = Math.floor(Number(N));
d = Math.floor(Number(d));
d = d<2 ? 2 : d>62 ? 62 : d;
var s, e = "";
while (N>0) {
s = N%d;
if (s>35) {
//36至62的数用a-z字符表示(s-36+97)
s = String.fromCharCode(s+61);
} else if (s>9) {
//10至35的数用A-Z字符表示(s-10+65)
s = String.fromCharCode(s+55);
}
e = s+e;
N = Math.floor(N/d);
}
e = e != "" ? e : "0";
return e;
}
//将d进制数转换为10进制
function jh_Dto10(str, d) {
d = Math.floor(Number(d));
d = d<2 ? 2 : d>62 ? 62 : d;
var code = 0, num = 0;
for (var i = 0; i<str.length; i++) {
code = str.charCodeAt(i);
if (code>96) {
code -= 61;
} else if (code>64) {
code -= 55;
} else {
code -= 48;
}
num += code*Math.pow(d, str.length-1-i);
}
return num;
}
//测试:
in_D = 62;
num1 = "15000";
num2 = jh_10toD(num1, in_D);
bt1.onRelease = function() {
num2 = jh_10toD(num1, in_D);
};
bt2.onRelease = function() {
num1 = jh_Dto10(num2, in_D);
};