How does JavaScript convert a string to decimal in a binary conversion?

Source: Internet
Author: User

JS is a very magical language, the internal system of many functions can help us make a number (into) system conversion;

JS can be directly used in the 16 system;

var a = 0xFF; 255

Converts arbitrary feed strings to decimal, such as binary, octal, hexadecimal, and number two, which is the most commonly used conversion to integer decimal;

The code is as follows:

parseint ("11", 2); 3 2-in-turn 10-in-system

parseint ("77", 8); 63 8-in-turn 10-in-system

parseint ("AF", 16); 175 16-in-turn 10-in-system

Converts 10 to 2, octal, hexadecimal strings

Object.ToString (N): Can (n) represent the system, such as

The code is as follows:

(152). ToString (2)//"10011000"; First, use parentheses to convert 152 to "package" into an object, or the following;

152..toString (2)//Here is the first point to convert 152 to float type decimal, and the second is to draw the object method;

152..toString (16)//"98": Decimal Turn 16 system

152..toString//"4o": Ten obtained turn 32

The same JavaScript support maximum feed is 36 (26 English letters + 10 digits)

35..toString//"Z": Maximum encoding "Z" is supported, case-insensitive

If necessary in the process of conversion. You can use the following methods:

The code is as follows:

/**

* @param num needs to be padded with 16 digits

* The number of digits @param len to be padded here is

* @returns a padded 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;

}

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.