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
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Numbers_and_strings
Numbers and strings - JavaScript | MDN
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.
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
JavaScript can't output non-string hex values, can it?
> (0xFF0000 + 0x0000FF).toString(16) "ff00ff" 32, 0x20, 0o40 (ES6), and 0b100000 (ES6) are the same number. JavaScript supports decimal, hexadecimal, octal, and binary numeric literals. If one of those representations is more convenient for what you want to do, you can use one of those. If you need a different representation when you output a result, you'll have to use toString with the desired base as parameter. More on reddit.com
๐ŸŒ r/javascript
13
2
November 3, 2015
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
๐ŸŒ
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);
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Number โ€บ toString
Number.prototype.toString() - JavaScript | MDN
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"
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Hexadecimal
Hexadecimal - Wikipedia
3 days ago - Let d be the number to represent in hex, and the series hihiโˆ’1...h2h1 be the hex digits representing the number. ... The following is a JavaScript implementation of the above algorithm for converting any number to a hex in String representation. Its purpose is to illustrate the above algorithm.
๐ŸŒ
HTML Color Codes
htmlcolorcodes.com
HTML Color Codes
Get inspired by our color library containing over 100+ shades from peach to periwinkle, turquoise to taupe, and just about everything in-between.Explore the libraryTry it ยท Color codes are ways of representing the colors we see everyday in a format that a computer can interpret and display. Commonly used in websites and other software applications, there are a variety of formats, including Hex color codes, RGB and HSL values, and HTML color names, amongst others. The most popular are Hex color codes; three byte hexadecimal numbers (meaning they consist of six digits), with each byte, or pair of characters in the Hex code, representing the intensity of red, green and blue in the color respectively.
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ javascript can't output non-string hex values, can it?
r/javascript on Reddit: JavaScript can't output non-string hex values, can it?
November 3, 2015 -

I'm aware that JS recognizes hex values, e.g. 0xFF0000, but am I correct in thinking that it can't actually output them as anything other than strings or decimal integers? In other words, you could hard code x to be equal to 0xFF0000, but you couldn't set it to that value dynamically, right?

Obviously you could set it to the decimal equivalent, or to a string containing the hex value (i.e. "0xFF0000"), and you can always use toString and parseInt with a base of 16 to translate back and forth, but you can't actually output hex-encoded integers directly. Am I right?

๐ŸŒ
JSON
json.org
JSON
A number is very much like a C or Java number, except that the octal and hexadecimal formats are not used. Whitespace can be inserted between any pair of tokens.
๐ŸŒ
ServiceNow Community
servicenow.com โ€บ community โ€บ developer-forum โ€บ how-to-convert-a-string-to-hex-on-the-server-side โ€บ m-p โ€บ 2844824
How to convert a string to hex on the server side - ServiceNow Community
February 29, 2024 - var inputString = "Welcome to ServiceNow"; var hexString = ""; for (var i = 0; i < inputString.length; i++) { var charCode = inputString.charCodeAt(i); // Convert character code to hexadecimal hexString += charCode.toString(16); } gs.info("Hex ->" + hexString);
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-convert-hexadecimal-to-decimal
JavaScript Program to Convert Hexadecimal to Decimal - GeeksforGeeks
August 5, 2025 - JavaScript recognizes the hexadecimal format when the "0x" prefix is present inside the Number() function. We will return the decimal equivalent of the hexadecimal input. Example: Demonstration of converting hexadecimal to decimal using the ...
๐ŸŒ
EndYourIf
endyourif.com โ€บ how-to-convert-decimal-to-hexadecimal-in-javascript
How to Convert Decimal to Hexadecimal in JavaScript | End Your If
May 30, 2023 - You can choose the method that best fits your needs, whether it's a simple conversion using the `toString()` method, the `Number.prototype.toString()` method, or adding leading zeroes with the `padStart()` method. With these techniques at your disposal, you can easily convert decimal numbers to hexadecimal in JavaScript, making your coding tasks more efficient.
๐ŸŒ
Code Beautify
codebeautify.org
Code Beautify and Code Formatter For Developers - to Beautify, Validate, Minify, JSON, XML, JavaScript, CSS, HTML, Excel and more
JSON Beautifier HTML Viewer Number to Words SQL Formatter Image to Base64 Base64 to Image HEX to Colortone Source Code Viewer Binary to Text JSON Viewer JSON Validator Base64 Decode Hex to Decimal XML Viewer XML to JSON Encryption-Decryption Excel to HTML CSS Validator XML Validator JavaScript Validator CSS Beautifier ONLINE JSON EDITOR Decimal to Hex Binary to Decimal ASCII to Text Random Emoji Generator REM to PX Converter Incorrect Quotes Generator Lua Beautifier
๐ŸŒ
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.