As it turns out your value: 76561198262006743 is much greater than Number.MAX_SAFE_VALUE
, which is (2^53 -1). This will lead to undefined behavior when trying to do arithmetic.
For example, on Google Chrome when typing just the number into the console and pressing enter, that value became 76561198262006740 (note the 3 at the end became a 0), which meant the hex conversion was incorrect as well.
An easy solution is, since you already have the value as a string, would be to perform your own decimal->hex conversion.
One such algorithm can he found here:
https://stackoverflow.com/a/21668344/8237835
A small comparison of the results of Number#toString(16)
and dec2hex(String)
:
function dec2hex(str){ // .toString(16) only works up to 2^53
var dec = str.toString().split(''), sum = [], hex = [], i, s
while(dec.length){
s = 1 * dec.shift()
for(i = 0; s || i < sum.length; i++){
s += (sum[i] || 0) * 10
sum[i] = s % 16
s = (s - sum[i]) / 16
}
}
while(sum.length){
hex.push(sum.pop().toString(16))
}
return hex.join('')
}
num = "76561198262006743"
console.log("dec2Hex: " + dec2hex(num))
console.log("toString: " + parseInt(num).toString(16))