Constructing a float32 value from four uint8 bytes
I am trying to read depth values from the topic /camera/depth/image_rect_color
. The message is of the type sensor_msgs/Image
and prints out 8 bit data (uint8
) as elements of an image array. Since the image is encoded as 32FC1
, I need to combine four of these elements to obtain depth information of a single pixel. A stack overflow answer contains the following code:
float f;
uchar b[] = {b3, b2, b1, b0};
memcpy(&f, &b, sizeof(f));
return f;
Is this the right approach to construct a float32
value from 4 bytes of uint8
values, without any data loss ?