2016-02-19 180 views
3

這可能聽起來像一個奇怪的掙扎,而且實際上很容易做到,但我找不到一種將字符串格式的十六進制轉換爲浮點的工作方式。NodeJS - 將十六進制轉換爲浮點數

我爲例是例如:406ea716

如果我用下面的一個網站轉換,我得到3.728948

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

我想盡單件的代碼,我發現在互聯網上,但不會返回相同的結果。

在NodeJS中是否存在執行相同轉換的模塊?如果不是,我該怎麼辦?

謝謝你的幫助。

+0

[轉換十六進制漂浮在javascript]的可能的複製(http://stackoverflow.com/questions/5055723 /轉換 - 十六進制到浮點數在javascript) – mentinet

回答

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); 
+0

謝謝你的回覆,但是這返回一個整數(1080993558),而如果我嘗試轉換這個十六進制與我列出的網站,它顯然會退縮浮動。 – mentinet

1

我有同樣的問題。嘗試這個。

緩衝液( '406ea716', '六角')。readFloatBE(0)

3.7289481163024902