在 JavaScript 中如何将大数字从 base6 转换为 base 16?

How do you convert big numbers from base6 to base16 in JavaScript?

如何在 JavaScript 中将一个大数(120 位)从 base6 转换为 base16?

parseInt(JavaScript 函数)有一个 problem with big numbers:

var big = 43535455245242542542542545353535345;
output1 = parseInt (big, 6);
output2 = output1.toString(16);
document.write (output2);

即使是这样的服务在 27 位数字后也不起作用:

https://www.w3resource.com/javascript-exercises/javascript-math-exercise-1.php https://www.unitconverters.net/numbers/base-6-to-base-16.htm

如何在 JavaScript 中完成?

在现代环境中,您可以使用 BigInt:

// I assume your starting point is a string
const big = "43535455245242542542542545353535345";
// Parse in base 6
const num = parseBigInt(big, 6);
// Convert to base 16 via `toString`:
const base16 = num.toString(16);
console.log(base16);

// There's no built-in parsing for base 6, so:
function parseBigInt(str, base = 10) {
    if (typeof base !== "number" || isNaN(base) || base < 2 || base > 36) {
        throw new Error(`parseBigInt doesn't support base ${base}`);
    }
    let num = 0n;
    base = BigInt(base);
    for (const digit of str) {
        num *= base;
        num += BigInt(parseInt(digit, 6));
    }
    return num;
}

在较旧的环境中,您可以使用 polyfill 或多个“大数字”库中的任何一个。

注意:我没有花任何时间来提高上述效率。只是功能性的。 :-)