Max Stern Max Stern - 1 month ago 12
Javascript Question

Parse HEX float

I have integer, for example,

4060
.

How I can get HEX float (
\x34\xC8\x7D\x45
) from it?

JS hasn't
float
type, so I don't know how to do this conversion.

Thank you.

Answer

If you want a hex string, try this:

> var b = new Buffer(4);
> b.writeFloatLE(4060, 0)
> b.toString('hex')
'00c07d45'

And the other way (using your input):

> Buffer('34C87D45', 'hex').readFloatLE(0)
4060.5126953125