5

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.

mentinet
  • 744
  • 3
  • 9
  • 23
  • Possible duplicate of [Converting hexadecimal to float in javascript](http://stackoverflow.com/questions/5055723/converting-hexadecimal-to-float-in-javascript) – mentinet Mar 07 '16 at 10:45

3 Answers3

8

I had the same issue. try this.

Buffer('406ea716','hex').readFloatBE(0)

3.7289481163024902

Rolland Saf
  • 91
  • 1
  • 3
0

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 );
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • Thank you for your reply, but this returns an integer (1080993558), while if I try to convert this hexadecimal with the websites I listed, it clearly returs a float. – mentinet Feb 19 '16 at 15:20
0

Have you tried parseInt?

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

$ node
> parseInt('406ea716', 16)
1080993558
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129