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.



The above answer is no longer valid. Buffer has been deprecated (see

New Solution:

function numToFloat32Hex(v,le)
    if(isNaN(v)) return false;
    var buf = new ArrayBuffer(4);
    var dv  = new DataView(buf);
    dv.setFloat32(0, v, true);
    return ("0000000"+dv.getUint32(0,!(le||false)).toString(16)).slice(-8).toUpperCase();

For example:

numToFloat32Hex(4060,true) // returns "00C07D45"
numToFloat32Hex(4060,false) // returns "457DC000"

Tested in Chrome and Firefox


If you want a hex string, try this:

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

And the other way (using your input):

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

UPDATE: new Buffer(size) has been deprecated, but it's easily replaced by Buffer.alloc(size):

var b = Buffer.alloc(4);


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.