Search code examples
javascriptpythonhexdecimalparseint

Converting hex strings to decimal format: Why am I getting different results in JavaScript and Python?


Getting different values converting the same hex string to decimal format in Javascript and Python!

Assume we have this hex string:

1f42c803ac5f267802916924e52a3e1b

We want to convert this string to decimal format. what can we do in JavaScript:

parseInt('1f42c803ac5f267802916924e52a3e1b', 16)

what can we do in Python:

int('1f42c803ac5f267802916924e52a3e1b', 16)

why these 2 function return different values?


Solution

  • You get different values when converting the exact hex string to decimal format in JavaScript and Python due to a difference in how the two languages handle large numbers. Python has no real limitation on how large a number can get. In JavaScript, you have the limitation of 32 bits (4 bytes) to store an integer.

    The parseInt function converts the hexadecimal string to a 32-bit signed integer in JavaScript. The maximum value that a 32-bit signed integer can represent is 2,147,483,647. The resulting value exceeds this maximum, so JavaScript truncates the bits beyond the 32nd bit, leading to a loss of precision. (Note how long your hexadecimal number is – the decimal version will be even longer, using more bits).

    The int function does not have the same limitation in Python. It can handle arbitrarily large integers without losing precision. So, when you convert the hex string to a decimal using int in Python, you get the correct and precise result.


    You can use the BigInt data type introduced in ECMAScript 2020 (ES2020) to obtain the correct result. BigInt allows you to work with arbitrarily large integers.

    const decimalValue = BigInt('0x1f42c803ac5f267802916924e52a3e1b');
    

    The resulting decimalValue will be a BigInt object, not a normal JavaScript number type.