Integer to 32-bit Float?

This is a discussion on Integer to 32-bit Float? within the C Programming forums, part of the General Programming Boards category; Hi, I know the very basics of C, however I'm having some trouble dealing with floats and converting values. I ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Integer to 32-bit Float?

    Hi,

    I know the very basics of C, however I'm having some trouble dealing with floats and converting values. I want my program to read a signed 16-bit integer and save the value as a 32-bit float in hexadecimal. Here is an example of what I need:

    Input a value: 2000
    32-bit float for this value equals 0x44FA0000

    Input a value: -2000
    32-bit float for this value equals C4FA0000

    And also the opposite:

    Input a float (in hex): 44FA0000
    The decimal value for this is 2000.

    So far my programs only did very simple operations, so I'm not sure how to deal with this. Can someone please shed some light or point to me some reference on what kind of functions/libs I need for this?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The details are here. You can probably do the first part with a simple assignment. The second will take a little more work (you have to know how to read in hex, and do some pointer casts).

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    scanf and the %x format specifier for printf for when you access the float through a char*.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    OK, I tried this. Why does the second part work but the first doesn't?

    Code:
    #include <stdio.h>
    
    int main()
    {
    int i;
     float f;               
    
     printf( "Enter an decimal integer: " );
     scanf( "%d", &i );    
     printf(" Value in float: %f\n", i);
    
     printf( "Enter a hex 32-bit float: " );
      scanf( "%x", &f );             
      printf( "Value in decimal: %f\n", f);
     
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well -- read your code. You read in an integer. You then re-interpret that integer's bit pattern as a floating point thing when you print it out. Why would you expect that to convert an integral value to the floating-point hex pattern that contains an equal value?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I think you should download one of the C draft standards in the sticky because some of these online references aren't really complete when it comes to format specifiers. For example, &#37;x is for an unsigned int, not a float.

    http://linux.die.net/man/3/strtof
    Last edited by robwhit; 09-10-2008 at 01:46 PM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I'm making an assumption, namely that sizeof(int)==sizeof(float)==4. If so, reading an integer that's supposed to represent a float hex-pattern and putting it in a float (part 2 above) works fine.

    To do part 1, you need to convert values, which is done with =.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Quote Originally Posted by robwhit View Post
    I think you should download one of the C draft standards in the sticky because some of these online references aren't really complete when it comes to format specifiers. For example, &#37;x is for an unsigned it, not a float.

    http://linux.die.net/man/3/strtof
    Actually he's got that part right. He reads in an integer into a 4 bit value that he later reinterprets as a float. The type of i and f have no bearing here, as long as they are 4 bytes. In fact, an array of 4 chars may be the best type since that's guaranteed to be 4 bytes.

    The results are predictable, but not well defined. Unfortunately I don't see a well defined way of doing this in C (unlike C++).
    Last edited by King Mir; 09-09-2008 at 08:39 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    BTW- why are you requiring the user to input an integer for part 1? Any decimal should be printable as a float. And that way you Part 1 is the exact inverse of part 2; just as in part 2 any hex sequence can be converted into a positive or negative decimal value, so would the inverse convert any positive or negative value to an IEEE floating point number.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Float Function Issues
    By GCNDoug in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2007, 03:25 PM
  3. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  4. Need more eyes to find problem??
    By sailci in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 09:03 PM

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