Aleks11

byteToFloat

Feb 23rd, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. // need to include math.h!!
  2.  
  3. float byteToFloat(char a, char b, char c, char d) {
  4.     int bin = (d << 24 | c << 16 | b << 8 | a);
  5.     char sign = (((bin & 0x80000000) >> 31) == 1)?-1:1;
  6.     float exp = pow(2.0f, ((bin & 0x7F800000) >> 23) - 127);
  7.     float mant = 1.0f;
  8.     for (int i = 22; i >= 0; i--) {
  9.         if ((bin >> i) & 1)
  10.             mant += 1.0f / pow(2.0f, 23 - i);
  11.     }
  12.     return sign * exp * mant;
  13. }
Advertisement
Add Comment
Please, Sign In to add comment