2

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.

Serg
  • 2,346
  • 3
  • 29
  • 38
Jay Bhatt
  • 41
  • 6

1 Answers1

1

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.

Befeepilf
  • 617
  • 4
  • 10
  • Thank You. I also found another solution here https://stackoverflow.com/questions/43239567/hex-string-to-int32-little-endian-dcba-format-javascript Need to replace getInt32() to getFloat32() for my scenario. – Jay Bhatt Jun 30 '20 at 04:44