How to convert a string to decimal _ JavaScript by using javascript in hexadecimal conversion

Source: Internet
Author: User
Javascript is a magic language that converts any hexadecimal string to decimal, such as binary, octal, and hexadecimal, if the second digit is not written, it is the most commonly used convert to integer decimal. JS is a magic language. Many internal functions can help us convert the number (in) format;

Javascript can be directly in hexadecimal notation;
Var a = 0xff; // 255.

Convert any hexadecimal string to decimal, such as binary, octal, and hexadecimal. If the second digit is not written, it is the most commonly used integer decimal;

The Code is as follows:


ParseInt ("11", 2); // 3 2 hexadecimal to 10 hexadecimal
ParseInt ("77", 8); // 63 convert the hexadecimal value to the hexadecimal value
ParseInt ("af", 16); // 175 hexadecimal to 10 hexadecimal


Converts a 10-hexadecimal string to a binary, octal, or hexadecimal string.
Object. toString (n): (n) represents the hexadecimal format, as shown in figure

The Code is as follows:


(152). toString (2) // "10011000"; Use brackets to convert "package" to an object, or write the following statement;
152 .. toString (2) // here the first point is to convert 152 to float decimal places, and the second point is the method of extracting objects;
152 .. toString (16) // "98": Convert decimal to hexadecimal
152 .. toString (32) // "4o": 10 hexadecimal to 32 hexadecimal

Similarly, Javascript supports a maximum hexadecimal value of 36 (26 letters + 10 digits)

35 .. toString (36) // "z": supports the maximum encoding "Z", case insensitive

If you need to complete the conversion process. You can use the following method:

The Code is as follows:


/**
* @ Param num the 16 to be filled is a number
* @ Param len the number of digits to be filled. Here is
* @ Returns string after completion
**/
Function format (num, len ){
Var l = num. length;
If (num. length <len ){
For (var I = 0; I <len-l; I ++ ){
Num = "0" + num;
}
}
Return num;
}

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.