简体   繁体   中英

How to convert 4 byte Hex to floating-point number in Node.js

I have four bytes of Hex data, I am trying to convert it to floating-point number in Node.js.

ie

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.

Given that you have a string representation of your hex data (eg '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 L ittle E ndian .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM