How to assemble a float from two bytes?

Viewed 564

I am currently working on a project where I need to read out a DHT11 humidity and temperature sensor. The communication between the MCU and the serial device is quite low-level, but I managed to receive the measured values (humidity+temperature) as a byte array of length 4 (the 5th byte is the checksum):

Values that I receive from the DHT11 sensor:

- byte[0] = humidity integer part
- byte[1] = humidity decimal part
- byte[2] = temperature integer part
- byte[3] = temperature decimal part
- byte[4] = checksum of the first four bytes

I now would like to convert byte[0] and byte[1] to a float and the same for the temperature (byte[2] and byte[3]). What is an efficient way to accomplish this on an Arduino Mega 2560 in C/C++ ?

Example:

byte[0] = 20 and byte[1] = 12 => 20.12 [float]
1 Answers
Related