I am using an Arduino to parse UDP packages sent from an application on my local network. The messages contain 36 bytes of information. The first four bytes represent one (single-precision) float, the next four bytes another one, etc.
The Arduino function udp.read() reads the data as chars, and I end up with an array
char data[36] = { ... };
I am now looking for a way to convert this into the corresponding nine floats. The only solution I have found is repeated use of this trick:
float f;
char b[] = {data[0], data[1], data[2], data[3]};
memcpy(&f, &b, sizeof(f));
However, I am sure there must be a better way. Instead of copying chunks of memory, can I get away with using only pointers and somehow just tell C to interpret b as a float?
Thanks
union Data
{
char data[36];
float f[9];
};
union Data data;
data.data[0] = 0;
data.data[1] = 0;
data.data[2] = 0;
data.data[3] = 0;
fprintf(stdout,"float is %f\n",data.float[0]);