Home >Web Front-end >JS Tutorial >How to Convert Decimal to Hexadecimal and Back in JavaScript?
In JavaScript, decimal values can be effortlessly converted into their hexadecimal equivalents. The core of this conversion process lies in manipulating numeric values using the toString() and parseInt() functions.
To embark on this conversion, invoke the toString() method on your decimal number, specifying 16 as the radix parameter. This will transform the number into its hexadecimal representation as a string:
hexString = yourNumber.toString(16);
Now, suppose you want to revert the process and retrieve the original decimal value from the hexadecimal string. To achieve this, employ the parseInt() function:
yourNumber = parseInt(hexString, 16);
This will parse the hexadecimal string back into a decimal number using the specified radix of 16. And there you have it - a seamless conversion between decimal and hexadecimal values in JavaScript.
The above is the detailed content of How to Convert Decimal to Hexadecimal and Back in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!