Reputation: 41
I have four bytes of Hex data, I am trying to convert it to floating-point number in Node.js.
i.e.
0x58 0x86 0x6B 0x42 --> 58.8812
0x76 0xD6 0xE3 0x42 --> 113.9189
0x91 0x2A 0xB4 0x41 --> 22.52078
I have tried to convert from different functions found on the internet, But unfortunately not getting the desired outcome. On https://www.scadacore.com/tools/programming-calculators/online-hex-converter/ link I am getting proper value in "Float - Little Endian (DCBA)" cell by entering Hex string, But do not know how to do it in node js. I think maybe I am searching the wrong thing or I have understood it wrong.
Thank You.
Upvotes: 2
Views: 1429
Reputation: 647
Given that you have a string representation of your hex data (e.g. '58866B42'
regarding your first example) do the following to convert it to a floating point number:
let myNumber = Buffer.from(hexString, 'hex').readFloatLE()
The LE
in readFloatLE
stands for Little Endian.
Upvotes: 1