Forum Discussion
Altera_Forum
Honored Contributor
15 years agoCombine 4 bytes in float
Hi all!
i'm receiving a float (32bit) from the serial port... i receive the correct sequence but now i'm trying to recombine the number with this code: unsigned char b1,b2,b3,b4; b1=GetUart(); b2=GetUart(); b3=GetUart(); b4=GetUart(); coeff = (float)((b4&0xFF) | ((b3<<8)&0xFF00) | ((b2<<16)&0xFF0000) | ((b1<<24)&0xFF000000)); i'm sending pigreco (for example) and receive the bytes: 0xdb,0x0f,0x49,0x40 that is the correct value, just swap. but if i print the value result coeff :3.675212e+09 any idea??3 Replies
- Altera_Forum
Honored Contributor
You need to cast the pointer, not the value try:
union { unsigned char b[4]; float f; } x; x.b[0] = GetUart(); ... coeff = x.f; - Altera_Forum
Honored Contributor
thanks!
i've never userd this function "union" by the way, the compiler say me: ../acquisition_6.c:78: error: `b' undeclared (first use in this function) ../acquisition_6.c:78: error: (Each undeclared identifier is reported only once ../acquisition_6.c:78: error: for each function it appears in.) ../acquisition_6.c:83: error: incompatible type for argument 1 of `printf' but the code is: --- Quote Start --- void ParseCoeff(){ [...] union { unsigned char b[4]; float f; } x; if (UartHasFourByte()){ b[1] = GetUart(); b[2] = GetUart(); b[3] = GetUart(); b[4] = GetUart(); coeff = x.f; [...] --- Quote End --- - Altera_Forum
Honored Contributor
solved the compiler error...
thanks!!