为什么 JavaScript base-36 转换似乎不明确

nph*_*nph 6 javascript base node.js base36 google-chrome-devtools

我目前正在编写一段使用 base 36 编码的 JavaScript。

我遇到了这个问题:

parseInt("welcomeback",36).toString(36)
Run Code Online (Sandbox Code Playgroud)

看来要回来了"welcomebacg"

我在 Chrome 开发人员的控制台和 Node.js 中对此进行了测试,结果相同。

这个结果有什么合乎逻辑的解释吗?

hev*_*ev1 6

的结果parseInt("welcomeback",36)大于Number.MAX_SAFE_INTEGER(2 53 -1),因此无法准确表示。一种可能的解决方法是BigInt手动执行基数转换。

const str = "welcomeback";
const base = 36;
const res = [...str].reduce((acc,curr)=>
   BigInt(parseInt(curr, base)) + BigInt(base) * acc, 0n);
console.log(res.toString());
console.log(res.toString(36));
Run Code Online (Sandbox Code Playgroud)