Convert from unsigned char array to float!

From: Goran (red_snake1_at_hotmail.com)
Date: 08/23/03


Date: 23 Aug 2003 08:17:43 -0700

Hi!

I need to convert from a unsigned char array to a float. I don't think
i get the right results in the program below.

unsigned char array1[4] = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in
what order the data is stored so i try both ways.
unsigned char array2[4] = { 0x23, 0x44, 0xc2, 0xde};

float *pfloat1, *pfloat2;

pfloat1 = (float *)array1;
pfloat2 = (float *)array2;

printf("pfloat1 = %f, pfloat2 = %f\n", pfloat1, pfloat2);

The result here is:
pfloat1 = 0.000000, pfloat2 = -6999176012340658176.000000

I know this data is stored in single-precision floatingpoint number.
Calculating this with the formula: 1.mantissa * 2^(exp-127) gets the
following results:
-6614457784713468934.9861376 when using array1
-79.784837 when using array2.

Anyone know why i get so bad results? I'm running this program under
Linux(Red Hat) on a Intel pentium machine.

Regards,
Goran



Relevant Pages

  • RE: REAL/DOUBLE PRECISION
    ... Real and float are approximate data types (not all vaules can be represented ... that behind SQL-Server Float is an IEEE Float representation. ... > Kind Regards ... > Justus Gadient ...
    (microsoft.public.sqlserver.programming)
  • Re: Convert from unsigned char array to float!
    ... Goran wrote: ... > I need to convert from a unsigned char array to a float. ... BTW: I'd use a union: ...
    (comp.os.linux.development.apps)
  • Re: sagulator for glass?
    ... but for shelving purposes, almost all float is interchangable. ...
    (rec.woodworking)
  • Re: float or doubles twos complement representation.
    ... byte format and reconstruct the same to float. ... store it into a byte array). ... you MUST make decoding exactly reverse ...
    (comp.lang.java.programmer)
  • Re: Float to string and vice versa
    ... There are an infinite number of values between .9999 and .99999 and with ... float or double, there are only a finite number of choices to represent them ... > CString strComfortValue; ...
    (microsoft.public.vc.mfc)