Convert Hex IEE 754 string to Decimal or float?

Hi All,

I’ve got a project where I’m reading the temperature of a Peltier controller using RS232. I send a temperature read command and I get that command echoed back followed by the temperature in Hex IEE 754 format.

Example:

I send: “$RN100?” - This looks for the temperature stored in register 100 of the Peltier controller.

Then i get “$RN100? 41C80580”

Doing the converstion of 41C80580 to decimal using this Website I get the number 25.002686 (the controller is set to control at 25 so this sounds right.)

I’m using Serial.readStringUntil() so my 41C80580 number is still a string in the Arduino.

So my question is how do i both convert the string to a long or a float and make sure it displays correctly for example 41C80580 displaying as 25?

Cheers

Steve

Edit:
Looked around on google a bit more and i can use:
long number = strtol( &hexstring[1], NULL, 16);
to change the hex string to a long. This is great it means I only need to find out how to convert my number to IEE 754 format now.

The Arduino compiler uses IEEE-754 single precision floating point so if you convert the hex string to binary it should be in the right format to interpret as a float. The strtol() function should do the conversion.

#include <stdlib.h>     /* strtol */

long l = strtol(hexstring, NULL, 16);
float f = *(float *)&l;  // Cast the address of the long as a float pointer and dereference

Note: You should spell I-Triple-E with three E’s. :slight_smile:

Yep good spot on the amount Es!

Yes. that’s exactly what I needed! Works brilliantly.

Gees, John...I would never have thought of that...