(TIP) The method to convert 4 bytes array to float

If you have 4 byte array, for example buffer[4], you can change the byte buffer to float.


union BitConvert{
float f;
unsigned long ul;

unsigned char a,b,c,d;
a = buffer[0];
b = buffer[1];
c = buffer[2];
d = buffer[3];

BitConvert EX;
EX.ul = (a << 24)  | (b << 16) | (c << 8) | d;
// or EX.ul = (d << 24) | ( c << 16)  | (b << 8) | a; (It is depend on your plaform.)

There is float value in the "EX.f".

Thank you~. ^^


Popular posts from this blog

OpenCV Stitching example (Stitcher class, Panorama)

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Optical Flow sample source code using OpenCV

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

(OpenCV Study) calcOpticalFlowFarneback example source code ( dense optical flow )

yuv422(YUYV) to RGB and RGB to yuv422(YUYV), (Using OpenCV and TBB)

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)