You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS中BigInt转十六进制字符串报错问题咨询

BigInt转十六进制字符串的正确方式

问题根源

BigInt的toString()方法要求基数参数是2到36之间的数字,你传入的'hex'是字符串类型,不符合参数规范,因此抛出RangeError。

解决代码

直接传入数字16作为基数参数即可:

let val = 67874000000000000n;
let val_hex = val.toString(16);
console.log(val_hex); // 输出: eace1e8000000

额外需求:带0x前缀

如果需要生成带标准十六进制前缀的字符串,手动拼接即可:

let val_hex_with_prefix = `0x${val.toString(16)}`;
console.log(val_hex_with_prefix); // 输出: 0xeace1e8000000

内容的提问来源于stack exchange,提问作者CODComputerAtWar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 00:44:54