Reputation: 784
That may sound like a weird struggle and actually easy to do, but I cannot find a working way to convert an hexidecimal in a string format into a float.
My exemple is for instance: 406ea716
If I convert it using one of the following website, I get 3.728948
.
http://www.h-schmidt.net/FloatConverter/IEEE754.html http://gregstoll.dyndns.org/~gregstoll/floattohex/
I tried every single piece of code I found on the internet, but it won't return the same result.
Does it exist a module in NodeJS to perform the same conversion? If not, what can I do?
Thank you for your help.
Upvotes: 5
Views: 4097
Reputation: 91
I had the same issue. try this.
Buffer('406ea716','hex').readFloatBE(0)
3.7289481163024902
Upvotes: 8
Reputation: 29014
Have you tried parseInt
?
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
$ node
> parseInt('406ea716', 16)
1080993558
Upvotes: 0
Reputation: 26871
No need for a module:
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 );
Upvotes: 0