Home > Net >  Number.toString returns "incorrect" hexadecimal value
Number.toString returns "incorrect" hexadecimal value

Time:10-01

In order to utilize a third party API, I have to convert the ID number to a hexadecimal. I converted the ID with Screenshot of public converter and JS script

CodePudding user response:

Your number is too big for JavaScript.

The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript.

I advise you to use the BigInt data type.

BigInt is a primitive wrapper object used to represent and manipulate primitive bigint values - which are too large to be represented by the number primitive.

Exampe:

// Your number in decimal
const decimalNumber = BigInt(10000000000000000);

// Your number in hex
const hexNumber = decimalNumber.toString(16);

console.log(`Decimal number: ${decimalNumber}`);
console.log(`Hex number: ${hexNumber}`);

  • Related