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.

1

3 Answers 3

8

I had the same issue. try this.

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

3.7289481163024902

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

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.
0

Have you tried parseInt?

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

$ node
> parseInt('406ea716', 16)
1080993558

1 Comment

Thank you for your reply but this is not the same result that the 2 websites I listed return.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.