Binary, octal, and hexadecimal conversion of JavaScript

Source: Internet
Author: User

Convert decimal to other hexadecimal

Objectname. tostring ([Radix])

  • Objectname is required. The object to be represented by a string.
  • Radix is optional. Specifies the hexadecimal format when the numeric value is converted to a string.

Returns the string representation of an object.

VaR M = 10;

Document. Write (M. tostring (2) + "<br>"); // 1010

Document. Write (M. tostring (8) + "<br>"); // 12

Document. Write (M. tostring (10) + "<br>"); // 10

Document. Write (M. tostring (16) + "<br>"); // It is displayed as a, note that it is in lower case.

The minimum value of Radix is 2 and the maximum value is 36 (expressed by 10 digits and 26 English letters ).

Convert others to decimal

Parseint (numstring, [Radix])

  • Numstring is required. String to be converted to a number.
  • Radix is optional. Between 2 and 36, it indicates the hexadecimal value of the number saved by numstring. If this parameter is not provided, the string prefixed with '0x 'is treated as hexadecimal, and the string prefixed with '0' is treated as octal. All other strings are treated as decimal.

Returns an integer converted from a string.

Document. Write (parseint (1010, 2) + "<br>"); // 10

Document. Write (parseint (12, 8) + "<br>"); // 10

Document. Write (parseint (10, 10) + "<br>"); // display as 10

Document. Write (parseint ("A", 16) + "<br>"); // The value is 10.

Document. Write (parseint ("A", 16) + "<br>"); // The value is 10.

From: http://www.cftea.com/c/2009/04/OY8JV5J7JDFYZ0UZ.asp

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.