javascriptparseintbigintstring-conversionbase36

JavaScript: parseInt(string, 36) converts different strings to same number


I have an issue with string conversion:

When using parseInt(string[,radix]) like so:

BigInt(parseInt('something', 36))

it outputs: 80920602611116n. with a different input like so:

BigInt(parseInt('somethink', 36))

it outputs: 80920602611120n, which is something else of course.

However if it's a longer string, the output number is the same sometimes:

BigInt(parseInt('thisisactuallyadifferentsomething', 36))
BigInt(parseInt('thisisactuallyadifferentsomethink', 36))

this would output 1867697451648055638757226289961051507749359223570432n for both, although they are different.

I know, that there is a limit for safe representation of integers, that's why I used BigInt, which has no limit.

I have to be able to differ strings of this size, while using BigInt to represent those.


Solution

  • See encode a big integer to base62, but replace the following base 62 digits...

    var digits = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    

    ...with the base 36 digits...

    var digits = '0123456789abcdefghijklmnopqrstuvwxyz';
    

    Hope this helps...