【发布时间】:2021-10-06 15:09:31
【问题描述】:
我有以下代码在 NodeJS 中将二进制转换为十六进制:
const bin = "1011100100001011101010100011100100001001101000100011101110110101101111011000001111111100010010111110001110101011100111101101110101100110110111000001111010101010110001110001110000110101001111111101000100110011111000010111110011001011000000001001010000100100"
const hex = parseInt(bin, 2).toString(16).toUpperCase();
console.log(hex)
它只返回:
B90BAA3909A23800000000000000000000000000000000000000000000000000
【问题讨论】:
-
我不确定,但这可能是“超出范围”错误。我认为
parseInt返回的数字/输入是大/长。输入更小,它可以工作。 -
我查看了来自“rapidtables.com/convert/number/binary-to-hex.html”的来源,他们在 npm 上使用了“BigNumber”包。
console.log((new BigNumber(bin, 2)).toString(16).toUpperCase())也许这在没有“BigNumber”的情况下有效,而不是使用 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: node.js math binary hex parseint