Use JavaScript For hexadecimal conversion and javascript hexadecimal conversion

Source: Internet
Author: User

Use JavaScript For hexadecimal conversion and javascript hexadecimal conversion

JS is a magical language. Many internal functions can help us convert data (in;

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;

ParseInt ("11", 2 );   // 3 2 hexadecimal to 10 hexadecimal
ParseInt ("77", 8 );   // 63 8-to-10
ParseInt ("af", 16 ); // 175 Hexadecimal to 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

(152). toString (2) // "10011000 "; Use parentheses to convert 152 to a "package" into 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 decimal 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:

/*** @ Param num the 16 to be filled is the number * @ param len the number of digits to be filled in. Here is the * @ returns completed string **/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 ;}






How to use javascript to convert decimal to hexadecimal

Function dec2hex (num) {if (typeof num! = 'Undefined') {return Number (num). toString (16 );}}

Implemented Using JavaScript: input any hexadecimal n and number d, convert it to a hexadecimal output

Function test (n, d ){
If (isNaN (n )){
Return "n must be a number! ";
}
/* Only consider numbers, because n can be random numbers.
If n = 16, ABCDEF can be used in hexadecimal notation.
So what do you want to use if you enter 100?
All situations where only numbers are taken into account. The input must be numbers, saving the trouble.
There are decimal places and so on */
If (isNaN (d )){
Return "d must be a number! ";
}
Var src = (d + ""). split ("");
Var res = 0;
For (I = 0; I <src. length; I ++ ){
If (src [I]> = n ){
Return d + "not" + n + "hexadecimal number ";
}
Res ++ = Math. pow (n, (src. length-1-i) * src [I];
}
Return res;
}

Alert (test (2,101); // Tip 5
Alert (test (2,102); // The system prompts that 102 is not a binary number.

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.