Monday, August 8, 2022

[FIXED] How would I use js to convert a decimal to number to a hex string?

Issue

Decimal input: 76561198291043943
Hex output: 110000113b74660
Hex expected output: 110000113b74667

For some reason, when I use the following snippet and execute it, the expected result does not occur, but when I use https://www.binaryhexconverter.com/decimal-to-hex-converter to convert a decimal number to a hex string, I get the expected output.

console.log(Number(76561198291043943).toString(16))


Solution

76561198291043943 is greater than Number.MAX_SAFE_INTEGER.

Use BigInt instead:

console.log(BigInt("76561198291043943").toString(16))



Answered By - Spectric
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.