float variable displaying in hexadecimal format using pointers

This is a discussion on float variable displaying in hexadecimal format using pointers within the C Programming forums, part of the General Programming Boards category; Iam trying to display the flaot variable in terms of hexadecimal in byte by byte format. Is this valid what ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108

    float variable displaying in hexadecimal format using pointers

    Iam trying to display the flaot variable in terms of hexadecimal in byte by byte format.

    Is this valid what iam doing here?

    Code:
    #include<stdio.h>
    
    int main()
    {
            float fVal = 10.1234;
            char *ptr;
            int i;
    
            ptr = &fVal;
    
    
            for(i = 0;i < 4;i++)
            {
                    printf("%02x \n",*ptr);
                    ptr++;
            }
    
            return 0;
    }
    output:

    72
    fffffff9
    21
    41

    iam trying to find out the float data variable bit pattern , till now not with much success ( i.e how10.1234 will be converted into bits and kept in that 4 bytes).if any body can find link or tutorial that would be really benificial for me.

    thanks for all the help friends

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by vlrk View Post
    iam trying to find out the float data variable bit pattern , till now not with much success ( i.e how10.1234 will be converted into bits and kept in that 4 bytes).if any body can find link or tutorial that would be really benificial for me.
    If you want to know the details of how a value, such as 10.1234 is converted into a pattern of 32 bits (4 bytes) then this may help:
    http://en.wikipedia.org/wiki/IEEE_fl...point_standard
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Someone I know showed me this trick a long time ago. I never entirely understood it though...

    Code:
    typedef unsigned int u32;
    typedef unsigned long long int u64; //AKA int64
    
        float tmpfloat;
        u64 *castfloat=(u64*)&tmpfloat;
        double tmpdouble;
        u64 *castdouble=(u64*)&tmpdouble;
        u64 InputValue;
        tmpfloat = 100.0;
        InputValue = *castfloat;
        InputValue &= 0xFFFFFFFF;
        printf("&#37;08X\n", InputValue);
        tmpdouble = 100.0;
        InputValue = *castdouble;
        printf("%I64X\n", InputValue);

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You might want to clean up the output a little:
    Code:
    unsigned char *ptr;
    ...
    ptr = (unsigned char *)&fVal;
    ...
    
    printf("&#37;02X \n",*ptr);
    'unsigned' so that negatives aren't, well, negative... And a capital 'X' in the format string so that you get nice capital hex digits. What I'm used to anyways.

    So...
    72 F9 21 41
    is equivalent to
    01110010111110010010000101000001

    Now you can start to decode each field - sign, exponent, mantissa...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by nonoob View Post
    So...
    72 F9 21 41
    is equivalent to
    01110010111110010010000101000001

    Now you can start to decode each field - sign, exponent, mantissa...
    Or
    01000001 00100001 11111001 01110010
    depending on byte order of the machine.

    Given the size of the number, I'd hazard a guess on the latter (1.0 I know is 3F800000, and 10.234 would be a bit bigger, but not dramatically so, since it's about 2^4 larger, which means 4121F972 is more likely than something starting with 72, near the limit of positive values, which makes the exponent large indeed).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by matsp View Post
    Or
    01000001 00100001 11111001 01110010
    depending on byte order of the machine.

    Given the size of the number, I'd hazard a guess on the latter (1.0 I know is 3F800000, and 10.234 would be a bit bigger, but not dramatically so, since it's about 2^4 larger, which means 4121F972 is more likely than something starting with 72, near the limit of positive values, which makes the exponent large indeed).

    --
    Mats
    72...near the limit of positive values? 42C80000 is 100.0, 43480000 is 200.0

    Nintendo 64 uses a lot of these. heheh

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    These responses are indeed helping me a lot.

    I request you to explain me the below lines which i found in "http://en.wikipedia.org/wiki/IEEE_floating-point_standard"


    Code:
    Next, the number (without the sign; i.e., unsigned, no two's complement) is converted to binary notation, giving 1110110.101. The 101 after the binary point has the value 0.625 because it is the sum of:
    
       1. (2 to the power of −1)  1, from the first digit after the binary point
       2. (2 to the power of −2)  0, from the second digit
       3. (2 to the power of −3)  1, from the third digit.
    iam trying to understand how 101 will become 625.

    friends please give your thoughts.

    thanks for all your inputs .

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    iam trying to understand how 101 will become 625.
    Rather, you are trying to understand how 0.101 in base 2 is 0.625 in base 10. But before you do that explain why 101 in base 2 is 5 in base 10.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    101 = (2 power of 2) * 1+ (2 power of 1) * 0 + (2 power of 0) * 1 = 4+0+1=5

    In this way

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Good. Notice that in converting 101 in base 2 to base 10, you began by multiplying with 2^2 (2 to the power of 2, not bitwise xor), then 2^1, and finally 2^0. Now, for 0.101 in base 2, do the same process, but multiply with 2^-1, 2^-2 and 2^-3, respectively.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    Now i got that , we have to take the fractional part if iam right here.


    i.e 2^-1= 1/2 = 0.5 * 1 = 0.5
    2^-2 = 1/4 = 0.25 * 0 = 0
    2^-3 = 1/8 = 0.125 * 1 = 0.125

    0.5+0+0.125=0.625

    is it correct .. ? or some other way

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Yes, that is correct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    108
    0.625 = 2^-4 also so why cannot it be 1000.

    where as to find the binary notation for any integer we do a 2 division and the remainder becomes binary notation.

    similarly for this fraction part what is the method used to know the binary notation.

    thanks friends

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    0.625 = 2^-4 also so why cannot it be 1000.
    2^-4 = 0.0625 != 0.625
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I don't know where your 0.625 comes from...
    Let's start again.

    10.1234 = 1.265425 * 2^3
    The computer scales the number down by whatever power of 2 it takes to make the number 1.nnnnnn. In this case, divided it by 8.
    The integer '1' is tossed since it's always present after this scaling, so we don't need to waste a bit.

    The binary representation of 1.265425 is
    1.01000011111100101110010
    but you didn't see the first '1'

    1 + (implicit)
    0.25 +
    etc.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  3. Reflective Factory Design
    By Shamino in forum Game Programming
    Replies: 4
    Last Post: 12-16-2005, 05:50 PM
  4. help w/another program!!!
    By edshaft in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2001, 10:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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