Considerations for the hexadecimal conversion of javascript parseInt () Functions

Source: Internet
Author: User

ParseInt (string, radix) has two parameters. The first string is the input value, and the second radix is the input value. The parameter radix can be ignored. The default value is 10, convert the numbers in hexadecimal notation to a decimal INTEGER (if not an integer, rounded down ).

The value range of radix is 2 ~ 36. If radix is 1 or radix> 36, the conversion result is NaN. If radix is 0 or other values are ignored, radix is 10 by default.

This function requires that the first parameter be a string. If it is not a string, it will be converted to a string. Moreover, before conversion, characters matching the character set used by the hexadecimal number on the left of the string parameter are truncated for conversion of the hexadecimal number.
Spaces at the beginning and end of a string parameter are ignored.

ParseInt (param, radix) is equivalent to parseInt (String (param). trim (), radix)
For example, parseInt (010) // 8, the processing process isCopy codeThe Code is as follows: 010 --> 8 // 0, which must start with eight Cores
8 --> '8'
ParseInt ('8') // ignore radix. The first input parameter is identified as a decimal number.

Copy codeThe Code is as follows: parseInt (0x10) // parseInt (16) returns decimal 16.
ParseInt () // parseInt ('8', 16), get 8
ParseInt () // parseInt ('8', 7), 8 is discarded if it exceeds the character range used by the 7-digit system, and the conversion result is NaN
ParseInt (0 x) // parseInt ('16', 8) returns 14
ParseInt (0 x) // parseInt ('19', 8) character 9 exceeds the range of characters used by the octal number, is discarded, and the conversion result is 1

If the first parameter is passed into the string directly and radix is ignored, then:
The string starts with 0 and is identified as octal by default (IE 9 is identified as hexadecimal, and ie 6-8 is recognized as hexadecimal)
The string starts with 0x and is identified as hexadecimal by default.
Otherwise, it is recognized as a decimal system.Copy codeThe Code is as follows: parseInt ('010 ') // 8, ie9 gets 10
ParseInt ('018') // 1, ie9 gets 18
ParseInt ('017') // 15, ie9 gets 15
ParseInt ('010 ', 8) // It is directly recognized as an octal conversion. The conversion result is 8.
ParseInt ('010 ', 7) // It is directly recognized as a 7-digit conversion result.
ParseInt ('010 ', 16) // It is directly recognized as hexadecimal and the conversion result is 16.
ParseInt ('0x10') // 16
ParseInt ('0x1g ') // 1
ParseInt ('0x1f') // 31
ParseInt ('0x10', 8) // It is directly recognized as octal. The character x exceeds the character range used by the octal number. The conversion result is 0.
ParseInt ('0x10', 15) // The Conversion Result is 0.
ParseInt ('0x10', 16) // 0x conforms to the hexadecimal number expression format and is correctly recognized as a hexadecimal number. The conversion result is the same as that of parseInt ('10', 16) is 16
ParseInt ('0x10', 17) // It is recognized as hexadecimal. The character x exceeds the character range used by the hexadecimal number. The conversion result is 0.
ParseInt (true) // NaN, note the difference between the usage of Boolean in the Number function, Number (true) // 1

Special notes
1/0 // Infinity
Infinity. toString () // 'infinity'
As a result, he came:Copy codeThe Code is as follows: parseInt (1/0, 18) // NaN
ParseInt (1/0, 19) // 18

Because character I is in the character set used by the 19-digit system, CHARACTER n and subsequent
ParseInt (1/0, 19) is actually parseInt ('I', 19)
In additionCopy codeThe Code is as follows: parseInt (NaN, 36) // 30191.

Because CHARACTER n is equivalent to decimal 23 in hexadecimal notation, and character a is equivalent to decimal 10
23*36*36 + 10*36 + 23 = 30191
ParseInt (1/0, 36) // 1461559270678, which is actually parseInt ('infinity ', 36)
ParseFloat does not have a hexadecimal parameter and can only process a decimal number.
ParseFloat. length // 1

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.