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. :)
For compatibility, please pad HEX strings to an even number of characters in length...
hexlify is padding the number with 0s
hexlify is padding the number with 0s
Formatting hexadecimal Number to short UUID in JavaScript - Stack Overflow
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);
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"
Using String.padStart:
(d).toString(16).padStart(2, '0')
The most obvious way would be to check if it's only one character, and if so, add a zero before it:
function d2h(d) {
var s = (+d).toString(16);
if(s.length < 2) {
s = '0' + s;
}
return s;
}
A less obvious way would be this:
function d2h(d) {
return (d / 256 + 1 / 512).toString(16).substring(2, 4);
}
But that's too tricky; I'd definitely stick with the first solution.
To convert a number to hex, you can use the built-in toString(16) function. Simple code:
function convert(integer) {
var str = Number(integer).toString(16);
return str.length == 1 ? "0" + str : str;
};
function to_rgb(r, g, b) { return "#" + convert(r) + convert(g) + convert(b); }
var color = to_rgb(r, g, b);
Just use the RGB values, like:
this.context.fillStyle = "rgb(0,255,255)";
Here is an easy way:
return ("0000" + (+d).toString(16)).substr(-4);
Or:
return ("0000" + (+d).toString(16)).slice(-4);
You can use sprintf http://www.diveintojavascript.com/projects/javascript-sprintf [Wayback Machine March 2021]
(Refers to an Open Source JavaScript library called sprintf.js, at https://github.com/alexei/sprintf.js)
I havent tried it yet but something like this should work:
sprintf("%.4X", d)
Convert a number to a hexadecimal string with:
hexString = yourNumber.toString(16);
And reverse the process with:
yourNumber = parseInt(hexString, 16);
If you need to handle things like bit fields or 32-bit colors, then you need to deal with signed numbers. The JavaScript function toString(16) will return a negative hexadecimal number which is usually not what you want. This function does some crazy addition to make it a positive number.
function decimalToHexString(number)
{
if (number < 0)
{
number = 0xFFFFFFFF + number + 1;
}
return number.toString(16).toUpperCase();
}
console.log(decimalToHexString(27));
console.log(decimalToHexString(48.6));
Run code snippetEdit code snippet Hide Results Copy to answer Expand