The utils.hexValue(hexString) method will serialize as a QUANTITY, which has. I leading 0’s except that 0 is 0x0. This should be what you want.

The safest way to handle this is hexValue(BigNumber.from(v).toHexString()). This will handle all sorts of edge cases for you. :)

🌐
StackHowTo
stackhowto.com › home › javascript int to hex with padding
javascript int to hex with padding - StackHowTo
Home » javascript int to hex with padding · In this tutorial, we are going to see how to convert decimal to binary, octal, or hexadecimal using the toString
🌐
Coolaj86
coolaj86.com › articles › convert-decimal-to-hex-with-js-bigints
Convert Decimal Strings to Hex with JS BigInts
December 12, 2018 - Obviously, however, there is a way to store negative BigInts, otherwise the JavaScript engine wouldn't be able to distinguish between them either. In the long-standing tradition of BigInts, as it were, the historical way to solve this problem is to pad a positive number with 0 if (and only if) it's ambiguous (meaning that the high-order bit is set).
Discussions

For compatibility, please pad HEX strings to an even number of characters in length...
For compatibility with other libraries it is sometimes necessary to prefix the HEX string with '0'. More on github.com
🌐 github.com
17
October 29, 2014
hexlify is padding the number with 0s
Reload to refresh your session. ... There was an error while loading. Please reload this page. ... There was an error while loading. Please reload this page. Something went wrong. There was an error while loading. Please reload this page. ... blockInHex becomes 0x0104fc, while it should be 0x104fc by adding that extra 0, Geth's hexutil.Uint64 errors out · Error: invalid argument 0: json: cannot unmarshal hex number with leading zero digits into ... More on github.com
🌐 github.com
3
1
June 23, 2021
hexlify is padding the number with 0s
Reload to refresh your session. ... There was an error while loading. Please reload this page. ... blockInHex becomes 0x0104fc, while it should be 0x104fc by adding that extra 0, Geth's hexutil.Uint64 errors out · Error: invalid argument 0: json: cannot unmarshal hex number with leading zero digits into ... More on github.com
🌐 github.com
3
June 23, 2021
Formatting hexadecimal Number to short UUID in JavaScript - Stack Overflow
I have a hexadecimal number in javascript. For display purposes, I would like to format the string as: ffffffff-ffff-ffff 00000000-0000-01ff (8 digits)-(4 digits)-(4 digits) with padded zeros on the More on stackoverflow.com
🌐 stackoverflow.com
🌐
TutorialsPoint
tutorialspoint.com › How-to-format-hexadecimal-Number-in-JavaScript
How to format hexadecimal Number in JavaScript?
June 17, 2020 - Modern JavaScript provides the ... uppercase hex: " + hex); </script> </body> </html> ... Use toString(16) for basic hex conversion and padStart() for modern padding....
🌐
IQCode
iqcode.com › code › javascript › javascript-convert-number-to-hex
javascript convert number to hex Code Example
October 31, 2021 - number = 255 h = parseInt(number, 10).toString(16) // Result: &quot;ff&quot; // Add Padding h = h.padStart(6, &quot;0&quot;) // Result: &quot;0000ff&quot; ... Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond. Sign up · Develop soft skills on BrainApps ...
🌐
GitHub
github.com › indutny › bn.js › issues › 22
For compatibility, please pad HEX strings to an even number of characters in length... · Issue #22 · indutny/bn.js
October 29, 2014 - You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. ... Would you please check new bn(...).toString(16) always returns an even number of HEX digits.
Author: indutny
🌐
GitHub
github.com › ethers-io › ethers.js › issues › 1702
hexlify is padding the number with 0s · Issue #1702 · ethers-io/ethers.js
June 23, 2021 - Hello I have a number: const block = 66812 const blockInHex = utils.hexlify(blockNumber) blockInHex becomes 0x0104fc, while it should be 0x104fc by adding that extra 0, Geth's hexutil.Uint64 errors out Error: invalid argument 0: json: ca...
Author: ethers-io
Top answer
1 of 7
59

I would do a two-step process:

1) convert number to 16 digit hex with leading zeros:

var i = 12345; // your number
var h = ("000000000000000" + i.toString(16)).substr(-16);

2) add dashes

var result = h.substr(0, 8)+'-'+h.substr(8,4)+'-'+h.substr(12,4);
2 of 7
15

Further to knabar's answer:

If your number is really a full 64 bits long you should be aware that javascript has only doubles, which top out at around 53 bits of precision. E.g.

var i = 0x89abcdef01234567; // a 64-bit constant
var h = ("000000000000000" + i.toString(16)).substr(-16); // "89abcdef01234800"

So you probably want to split this into two 32-bit numbers, and format them 8 digits at a time. Then the second caveat strikes: javascript performs bitwise ops on signed 32-bit integers, and this formatting code can't handle negative numbers.

var i = 0xffd2 << 16; // actually negative
var h = ("0000000" + i.toString(16)).substr(-8); // "0-2e0000"

Since it's fairly likely that numbers you want formatted in hexadecimal are the result of bitwise manipulations, the code can be tweaked to print in two's complement instead:

var i = 0xffd2 << 16; // actually negative
var h = ("0000000" + ((i|0)+4294967296).toString(16)).substr(-8); // "ffd20000"

This produces the hex representation of the bottom 32 bits of the integral part of arbitrary positive and negative numbers. This is probably what you want (it's approximately printf("%08x")). Some more corner cases:

var i = 1.5; // non-integers are rounded
var h = ("0000000" + ((i|0)+4294967296).toString(16)).substr(-8); // "00000001"

var i = -1.5; // rounding is towards zero
var h = ("0000000" + ((i|0)+4294967296).toString(16)).substr(-8); // "ffffffff"

var i = NaN; // not actually a number
var h = ("0000000" + ((i|0)+4294967296).toString(16)).substr(-8); // "00000000"
Find elsewhere
🌐
Snipplr
snipplr.com › view › 42449
Convert Decimal to Hex - JavaScript Snipplr Social Repository
October 17, 2010 - Convert rgb to hex · 2018 · Calculate ... folder(s) Taken online · Expand | Embed | Plain Text · Copy this code and paste it in your HTML · function decimalToHex(d, padding) { var hex = Number(d).toString(16); padding = typeof (padding) === "undefined" || padding === null ...
🌐
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 - In some cases, you may want to ensure that the resulting hexadecimal representation has a fixed length, padding it with leading zeroes if necessary. We can achieve this by using the padStart() method.
🌐
CodePen
codepen.io › kevingimbel › pen › eYjYVy
JavaScript: Number to Hex String
*/ .hacky { width: 10em; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); color: red; &:before { content: 'click inside to select the current color'; font-size: 12px; font-weight: bold; position: absolute; top: -4.2em; left: -50%; color: #000; // fallback color: currentColor; /* 1 */ background: #fff; padding: .25em; } } ... /* * Return a random number within the defined range, * random(0, 5) would return a number between 0 and 5. */ function random(max, min) { return Math.floor((Math.random() * (max - min)) + min) } /* Takes an Object with RGB numbers like rgb = { r: 123, g: 233, b: 12 } Number must be within the RGB range (0-255). Each number is then converted to hexadecimal with 123.toString(16) which return the Hex Code for that number.
🌐
egghead.io
egghead.io › lessons › javascript-create-a-function-to-convert-js-numbers-into-css-hex-colors
Create a Function to Convert JS Numbers into CSS Hex Colors | egghead.io
JavaScript numbers and CSS hexadecimal numbers don't play very well together. You can solve this with a conversion function that takes the number, converts it to a string, then pads the string with the necessary zeroes at the start using padStart.
Published: March 23, 2020
🌐
xjavascript
xjavascript.com › blog › how-can-i-convert-an-integer-to-hex-with-a-fixed-length-in-javascript
How to Convert an Integer to Fixed-Length Hex in JavaScript (4-Digit Example) — xjavascript.com
Converting an integer to a 4-digit fixed-length hex string in JavaScript involves three key steps: converting to hex, padding with leading zeros, and truncating overflow.
🌐
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.