NodeJS - 将十六进制转换为float

时间:2016-02-19 15:03:56

标签: node.js hex

这听起来像是一场奇怪的挣扎,实际上很容易做到,但我找不到一种将字符串格式的十六进制转换为浮点数的工作方式。

我的例子是:406ea716

如果我使用以下网站之一转换它,我会得到3.728948

http://www.h-schmidt.net/FloatConverter/IEEE754.html http://gregstoll.dyndns.org/~gregstoll/floattohex/

我尝试了在互联网上找到的每一段代码,但它不会返回相同的结果。

NodeJS中是否存在执行相同转换的模块?如果没有,我该怎么办?

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

我有同样的问题。试试这个。

缓冲液( '406ea716', '六角')。readFloatBE(0)

  

3.7289481163024902

答案 1 :(得分:0)

无需模块:

var hex = '406ea716';
// transform the hexadecimal representation in a proper js hexadecimal representation by prepending `0x` to the string
// parseInt() - because your example was an integer.
var num = parseInt( '0x' + '406ea716');
console.log( num );

答案 2 :(得分:0)

您是否尝试过parseInt

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt

$ node
> parseInt('406ea716', 16)
1080993558
相关问题