Convert a number to a hexadecimal string with:

hexString = yourNumber.toString(16);

And reverse the process with:

yourNumber = parseInt(hexString, 16);
Answer from Prestaul on Stack Overflow
🌐
GitHub
gist.github.com › agirorn › 0e740d012b620968225de58859ccef5c
Convert decimal to hex in JavaScript. · GitHub
JavaScript already offers a "native way" of doing it: // our decimal number const nr = 999999999; // convert to hex const hex = nr.toString(16); // back to dec: const decNr = parseInt(hex, 16);
Discussions

solidity - How do you change an integer to a hex in javascript / web3.js? - Ethereum Stack Exchange
I am currently trying to create an automater for running scripts. It needs to have two values, lastFromBlock and currentToBlock which need to increment by about 1e5 with each process, so I need to More on ethereum.stackexchange.com
🌐 ethereum.stackexchange.com
December 8, 2022
Iterating over a range in hex
tl;dr how do I create a for loop in which I can call a function: foo('\x01') to foo('\x21') ? Just in case you don't see my answer further below: for n in range(0x22): c = chr(n) foo(c) More on reddit.com
🌐 r/Python
12
1
August 5, 2014
Is there a way to modify HEX color codes in CSS?
You'll need to use either SASS or JavaScript to accomplish what you're describing. CSS doesn't provide a way to apply alpha to a non-alpha hex color. You could leverage the opacity style attribute, but it's extremely unlikely that the document was set up with this in mind and you'll end up making things transparent that shouldn't be. What you can do is convert your color variables from hex to rgb and apply opacity independent of the variable like so: --color-gold: 191,158,93; background-color: rgba(var(--color-gold),.25); More on reddit.com
🌐 r/web_design
12
7
March 27, 2023
Is it possible to programmatically calculate a hex color based on another hex color?
Yes. Each pair of characters represents values 0 to 255 of red, green, and blue in hexadecimal. #RRGGBB FF is 255, so that's pure red (when green and blue are 0 - FF0000). Reducing the value of that pair will make the red darker until it gets to 00 (000000) which is black. https://coolors.co/ff0000-aa0000-990000-550000-000000 More on reddit.com
🌐 r/css
10
5
May 14, 2022
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-number-to-hex
Convert a number to Hexadecimal in JavaScript | bobbyhadz
The toString method will return the string representation of the number in hexadecimal form.
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › number › decimal to hexadecimal
JavaScript - Convert decimal number to hexadecimal - 30 seconds of code
September 21, 2022 - Numeric values are represented in decimal format by default, when converted to strings. If you want to display them in hexadecimal format, you can use Number.prototype.toString() and pass the base you want to use (16) as an argument.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-decimal-to-hex-in-javascript
How to convert decimal to hex in JavaScript ? - GeeksforGeeks
July 11, 2025 - Given a number and the task is to convert the number from decimal to hex. This can be done by using toString() method. It takes the parameter which is the base of the converted string.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Numbers_and_strings
Numbers and strings - JavaScript - MDN Web Docs - Mozilla
August 26, 2025 - The only exceptions are the backslash (\, which starts an escape sequence), the quote character being used to enclose the string, which terminates the string, and the newline character, which is a syntax error if not preceded by a backslash. More advanced strings can be created using escape sequences: The number after \x is interpreted as a hexadecimal number.
🌐
Medium
medium.com › @saumya07013 › exploring-the-world-of-javascript-hexadecimal-operations-7501a24cc3c8
Exploring The World Of Javascript Hexadecimal Operations | by Saumya | Medium
October 1, 2024 - JavaScript provides methods to convert between decimal and hexadecimal. The toString() method allows you to convert a number to a hexadecimal string by passing 16 as the argument:
Find elsewhere
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to convert decimal to hex in javascript
How to Convert Decimal to Hex in JavaScript - Coding Beauty
May 10, 2023 - To convert a decimal to hex in JavaScript, call the toString() method on the decimal, passing 16 as the radix argument, i.e., num.toString(16). The toString() method will return the string representation of the number in hexadecimal form.
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-decimal-to-hex-in-javascript
How to convert decimal to hex in JavaScript?
April 21, 2023 - The toString(16) method is the preferred approach for converting decimal to hexadecimal in JavaScript due to its simplicity and efficiency.
🌐
Nesin
nesin.io › blog › convert-number-to-hex-javascript
How to convert Number to Hexadecimal String in Javascript
March 21, 2023 - 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.
🌐
W3docs
w3docs.com › home › code snippets › javascript › how to convert decimal to hexadecimal in javascript
How to Convert Decimal to Hexadecimal in JavaScript | W3docs
It is done with the <kbd class="highlighted">toString()</kbd> method. This method takes the parameter, which is the base of the converted string. The base is 16. In this tutorial, we will show you how simple can be converting decimal number ...
🌐
Jamesqquick
jamesqquick.com › blog › convert-number-from-decimal-to-hexadecimal
Convert Number from Decimal to Hexadecimal in JavaScript (and visa versa)
June 18, 2020 - If you have a decimal number that you want to convert to hexidecimal, you can call the toString() function with the number and pass in a target base (binary, decimal, hexedecimal, etc.). This parameter is known as the radix.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Uint8Array › toHex
Uint8Array.prototype.toHex() - JavaScript - MDN Web Docs
The toHex() method of Uint8Array instances returns a hex-encoded string based on the data in this Uint8Array object. This method creates strings from a byte array. To convert individual numbers into hex, use the Number.prototype.toString() method with radix set to 16 instead.
🌐
W3Schools
w3schools.com › js › js_numbers.asp
JavaScript Numbers
JavaScript interprets numeric constants as hexadecimal if they are preceded by 0x. ... Never write a number with a leading zero (like 07). Some JavaScript versions interpret numbers as octal if they are written with a leading zero.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-convert-decimal-values-to-hexadecimal-in-javascript.php
How to Convert Decimal Values to Hexadecimal in JavaScript
You can simply use the toString() method with a radix or base of 16 to convert the decimal values to their hexadecimal equivalent in JavaScript.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript decimal to hex
How to Convert Decimal to Hexadecimal Number in JavaScript | Delft Stack
March 11, 2025 - You can convert a negative decimal number to hexadecimal by first converting its absolute value and then adding a negative sign to the result. Is there a built-in JavaScript function for hexadecimal conversion?
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toString
Number.prototype.toString() - JavaScript - MDN Web Docs
July 10, 2025 - The toString() method of Number values returns a string representing this number value. function hexColor(c) { if (c < 256) { return Math.abs(c).toString(16); } return 0; } console.log(hexColor(233)); // Expected output: "e9" console.log(hexColor("11")); // Expected output: "b"