12/29/2011

(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.


ex)

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~. ^^

No comments:

Post a Comment