JS Numerical binary Mutual transfer

Source: Internet
Author: User

Decimal conversion to binary:

var num = 100;console.log(num.toString(2));

The ToString () method converts a Number object to a string and returns the result.

Grammar

NumberObject.toString(radix);

Among them, Radix is optional. An integer that specifies the cardinality of the number that makes 2 ~ 36. If this argument is omitted, cardinality 10 is used. Note, however, that if the parameter is a value other than 10, the ECMAScript standard allows the implementation to return any value.

return value

A string of numbers. For example, when Radix is 2 o'clock, Numberobject is converted to a string represented by a binary value.

Thrown

The TypeError exception is thrown when the object calling the method is not number.

Binary goto decimal:

var num = 1100100;console.log(parseInt(num,2));

The parseint () function parses a string and returns an integer.

Grammar

parseInt(string, radix);

where string is required. The string to be parsed. Radix is optional. Represents the cardinality of the number to parse. The value is between 2 and 36. If this argument is omitted or its value is 0, the number is parsed based on 10. If it starts with "0x" or "0X", it will have a base of 16. If the parameter is less than 2 or greater than 36, parseint () returns NaN.

return value

Returns the parsed number.

Description

When the value of the parameter radix is 0, or if the parameter is not set, parseint () determines the cardinality of the number based on string.
For example, if string starts with "0x", parseint () resolves the remainder of the string to a 16-binary integer. If string starts with 0, then ECMAScript V3 allows an implementation of parseint () to parse subsequent characters into octal or hexadecimal digits. If string starts with a number from 1 to 9, parseint () parses it into a decimal integer.

Hints and Notes

Note: only the first number in a string is returned.
Note: spaces at the beginning and end are allowed.
Tip: If the first character of a string cannot be converted to a number, parsefloat () returns NaN.

parseint (NUM,8);Octal goto Decimalparseint (NUM,16);Hexadecimal goto Decimalparseint (num). toString (8)Decimal Turn octalparseint (num). toString (16)Decimal Turn hexparseint (NUM,2). ToString (8) //binary to octal parseint (Num,2). ToString (16) //binary turn hex parseint (Num,8). ToString ( 2) //octal to binary parseint (Num,8). ToString (16) //octal to hex parseint (Num,16). ToString (2) //hex-binary parseint (Num,16). ToString (8) //hex to octal        

JS numeric binary cross-transfer

Related Article

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.