Published on

How to convert Number to Hexadecimal String in Javascript

Authors

Converting numbers is straightforward similar to that of converting Hex to Number

Here is how to do it

const input = 127;

const hexNumber = input.toString(16) // '7f'

const parsedNumber = parseInt(hexNumber, 16) // 127

// Note: Directly using toString in a number like 127.toString() throws an error.
// Instead, wrap it in a bracket instead.

Basically, when we convert a number to a string using toString we are passing 16 to use hexadecimal notation

Happy playing-with hex!

Reference