Skip to content Skip to sidebar Skip to footer

Javascript Hexadecimal String To Ieee-754 Floating Point

I've looked around Stackoverflow for a few days trying to solve this problem and played in JsFiddle for a while but no luck so far. Here's my problem: i receive a base64 encoded st

Solution 1:

Unpacking a float from a word using DataView:

> v = newDataView(newArrayBuffer(4))
> v.setUint32(0, 0x40e9b76a)
> v.getFloat32(0)
7.3036394119262695

(Recommended over Uint32Array and Float32Array because it does not inherit the platform's endianness.)

You could also use this to swap the two 16-bit units:

> v.setUint32(0, 0xb76a40e9)
> ((hi, lo) => {
    v.setUint16(0, hi);
    v.setUint16(2, lo);
  })(v.getUint16(2), v.getUint16(0))
> v.getUint32(0).toString(16)
'40e9b76a'

Post a Comment for "Javascript Hexadecimal String To Ieee-754 Floating Point"