Hex to float problem

This is a discussion on Hex to float problem within the C Programming forums, part of the General Programming Boards category; hai, I suspect something wrong i have 32 bit data, its float in hex if i have 0x00000000 data then ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    India
    Posts
    11

    Hex to float problem

    hai,

    I suspect something wrong

    i have 32 bit data, its float in hex
    if i have 0x00000000 data then it corresponding float value is 0.0 for this i tried like this

    Code:
    unsigned char ch[]={0x00,0x00,0x00,0x00};
    unsigned long data=0;
    
    while(i<4){
    data=data<<8|ch[i];
    i++;
    }
    f=(1-(2*(data>>31)))*pow(2,((data>>23 & 0x0FF) -127))*(1+(float)(data & 0x007FFFFF)/0x800000); // convert hex to float
    
    //successfully converted to float
    
    printf("float value f = %f \n",f);// o/p>> i get  f=0.0
    
    
    if(f<=0.0)
    printf("less than 0r equal to 0 ");
    
    else
    printf("greater than 0");
    o/p i get as greater than 0
    but when i changed the if(f<=0.0) to if(f<=0.001), i get what i expect. i.e less than 0.

    i also tried with if(f<=(float)0.0) the result is same greater than 0.

    I wonder what cud be the reason, is OS changing float hex data from 0x00000000 to something else

    Any light on this !!!


    thanks

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Why is that strange? I haven't check your code, but you say you get less than 0.001 but not less than 0.0. So the result might be from 0.0 to 0.001.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  3. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 09:17 AM
  4. Integration problem
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 01-23-2006, 03:07 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21