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 https://nodejs.org/api/buffer.html#buffer_new_buffer_size).
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