user50862: I am reading data from an energy meter over modbus protocol and I am getting hex values from there. I want to convert that hex string to floating point value.
For example - 0x416f0000 denotes a value of 14.9375.Reference
Can anyone please help me how to do it on Arduino IDE?

user7508: If you do indeed have an ASCII string of hexadecimal numbers (and not a
raw binary float), then your best option may be to do the conversion in
two steps:

convert the string to a 32 bit unsigned number using
strtoul()
reinterpret the binary pattern of this integer as a float.

For example, the following program:
void setup() {
    const char modbus_data[] = "416F0000";
    union {
        uint32_t i;
        float f;
    } data;
    data.i = strtoul(modbus_data, NULL, 16);

    Serial.begin(9600);
    Serial.println(data.f, 4);
}

void loop(){}

prints 14.9375.
Note that for strtoul() to function properly, the hex string must be
followed by a character that is not a valid hex digit (a \0 in the
example above).

50862: <p>I am reading data from an energy meter over modbus protocol and I am getting hex values from there. I want to convert that hex string to floating point value.
For example - 0x416f0000 denotes a value of 14.9375.<a href="https://gregstoll.dyndns.org/~gregstoll/floattohex/" rel="nofollow noreferrer">Reference</a></p>

<p>Can anyone please help me how to do it on Arduino IDE?</p>
 7508: <p>If you do indeed have an ASCII string of hexadecimal numbers (and not a
raw binary float), then your best option may be to do the conversion in
two steps:</p>

<ol>
<li>convert the string to a 32 bit unsigned number using
<a href="https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#gaea44aa48bda8261f794dcb2d1e7ab2b2" rel="nofollow noreferrer"><code>strtoul()</code></a></li>
<li>reinterpret the binary pattern of this integer as a float.</li>
</ol>

<p>For example, the following program:</p>

<pre class="lang-c++ prettyprint-override"><code>void setup() {
    const char modbus_data[] = "416F0000";
    union {
        uint32_t i;
        float f;
    } data;
    data.i = strtoul(modbus_data, NULL, 16);

    Serial.begin(9600);
    Serial.println(data.f, 4);
}

void loop(){}
</code></pre>

<p>prints <code>14.9375</code>.</p>

<p>Note that for <code>strtoul()</code> to function properly, the hex string must be
followed by a character that is not a valid hex digit (a <code>\0</code> in the
example above).</p>
