Convert Serial Bytes to Float in Embedded C with Unions and Endianness
This article explains how to reconstruct a 32‑bit floating‑point value from four serial bytes in embedded C by using a union or struct overlay, demonstrates the code, shows the resulting little‑endian byte order, and provides a simple function to detect system endianness.
Converting a 4‑byte sequence to a float
In many embedded systems a floating‑point value such as float a = 231.5f occupies four bytes. In memory the value is stored as the IEEE‑754 pattern 0x43678000. When a serial interface receives the four bytes {0x43, 0x67, 0x80, 0x00} the program must reconstruct the original float.
Using a union to share the same memory
A convenient way is to define a union that contains a float member and an array of four unsigned char. The two members occupy the same address range, so writing to one member can be read through the other.
typedef union {
float f;
unsigned char s[4];
} Union_test;Example program that stores a float in the union, then copies the byte array into a struct and prints the recovered value:
#include <stdio.h>
typedef union {
float f;
unsigned char s[4];
} Union_test;
typedef struct {
float f1;
} Struct_test;
int main(void) {
float a = 231.5f;
Union_test x;
Struct_test z;
x.f = a; // write the float
z = *(Struct_test *)(&(x.s)); // reinterpret the byte array as a struct
printf("z=%.2f
", (double)z.f1);
return 0;
}Running the program prints z=231.50, confirming that the byte sequence 00 80 67 43 (little‑endian order) represents the original float.
Endianess of multi‑byte objects
Little‑endian systems store the least‑significant byte at the lowest address, while big‑endian systems store the most‑significant byte first. For the 32‑bit value 0x01234567 the layout is:
Little‑endian: 67 45 23 01 Big‑endian:
01 23 45 67Runtime detection of endianess
The following helper function examines the first byte of an integer to determine the host byte order:
void test_endian(void) {
int a = 1;
unsigned char *start = (unsigned char *)&a;
if (*start == 1)
printf("Little‑endian
");
else
printf("Big‑endian
");
}On typical x86/ARM development boards the function prints Little‑endian, matching the byte order observed in the float conversion example.
Alternative: reinterpretation via a struct
The same result can be obtained by casting the address of the byte array to a struct that contains a single float member. The code shown above demonstrates this technique; after the cast the printed value is again 231.50.
These examples illustrate how unions or explicit pointer casts can be used to translate a raw byte stream received over a serial link into a floating‑point value, while also highlighting the importance of understanding the target platform’s endianess.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
