#include <Arduino.h>
union FloatBytes {
float floatValue;
u_char bytes[4];
} u;
void setup() {
Serial.begin(115200);
}
void loop() {
// Example data (replace with your own four bytes)
byte dataBytes[] = {0x43, 0x62, 0xbb, 0x6b}; // Equivalent to the float value 12.34 (in little-endian order)
// Convert four bytes to float
u.bytes[3] = dataBytes[0];
u.bytes[2] = dataBytes[1];
u.bytes[1] = dataBytes[2];
u.bytes[0] = dataBytes[3];
// Access the float value
float result = u.floatValue;
float f;
u_char b[] = {0x43, 0x62, 0xbb, 0x6b};
memcpy(&f, &b, sizeof(f));
// Print the result
Serial.print("Original Bytes: ");
for (int i = 0; i < 4; i++) {
Serial.print(dataBytes[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Converted Float: ");
Serial.println(result, 2); // Display with 2 decimal places
delay(1000); // Adjust delay as needed
}