2-in-system ~62 and 10-in-system conversion

Source: Internet
Author: User
Transformation

The effect is as follows:

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);
Converts the 10 binary number n to the D number (in 0-9,a-z,a-z characters) and D to 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) {
The number 36 to 62 is represented by a-Z character (s-36+97)
s = String.fromCharCode (s+61);
else if (s>9) {
The number 10 to 35 is represented by A-Z character (s-10+65)
s = String.fromCharCode (s+55);
}
e = s+e;
N = Math.floor (n/d);
}
E = e!= ""? E: "0";
return e;
}
Converts a D-number into a 10-system
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;
}
Test:
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);
};



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.