"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > JavaScript decimal and hexadecimal conversion techniques

JavaScript decimal and hexadecimal conversion techniques

Posted on 2025-04-18
Browse:455

How Do I Convert Between Decimal and Hexadecimal in JavaScript?

Decimal to Hexadecimal Conversion in JavaScript

Converting decimal values to hexadecimal equivalents is a common task in programming. In JavaScript, this can be done with ease using the toString(base) method.

Conversion to Hexadecimal String

To convert a decimal number to its hexadecimal string representation, use the following syntax:

hexString = yourNumber.toString(16);

For example, to convert the decimal number 10 into hexadecimal, we would write:

let hexString = 10.toString(16);

This will assign the string "a" to the hexString variable, as "a" is the hexadecimal representation of 10.

Conversion from Hexadecimal String

To convert a hexadecimal string back to its decimal equivalent, use the parseInt(string, base) function:

yourNumber = parseInt(hexString, 16);

For example, to convert the hexadecimal string "a" to its decimal equivalent, we would write:

let yourNumber = parseInt("a", 16);

This will assign the value 10 to the yourNumber variable.

Remember that the base parameter in both methods specifies the base of the number being converted. For hexadecimal, always set the base to 16.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3