Printing a float's bytes...

This is a discussion on Printing a float's bytes... within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main() { float f = -118.625f; char* c = (char*)&f; unsigned int i = 0; for ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64

    Printing a float's bytes...

    Code:
    #include <stdio.h>
    
    int main() {
      float f = -118.625f;
      char* c = (char*)&f;
      unsigned int i = 0;
      for (i = 0; i < sizeof(float); ++i) {
        printf("%u\n", c[i]);
      }
      getchar();
      return 0;
    }
    1) I made this code based on the article at Wikipedia (http://en.wikipedia.org/wiki/IEEE_754).
    2) As far as I know, my machine is little endian.
    3) According to 1), the binary representation for -118.625f is 11000010 11101101 01000000 00000000.
    4) From 2) and 3), I expected my code above to print the values of those bytes - the first one would be 0 (00000000), the second one would be 64 (01000000), the third one would be 237 (11101101) and the last one would be 194 (11000010).

    However, when I compile and execute the code above, it gives me this output:

    0
    64
    4294967277
    4294967234

    You've probably guessed by now what my question is: why isn't the output the same as I expected it to be?

    Thank you in advance.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Try making your "char" into "unsigned char", or perhaps, use a "int *p = (int *)&f" and print it as with "%08x" for a 32-bit hex number.

    --
    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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    Coimbra, Portugal
    Posts
    64
    Oh, but of course! Having an unsigned char* instead of a char* did the trick. So, basically, it was converting a signed type to an unsigned type, which gave me unexpected results.

    Thanks for the quick reply.
    Name: Miguel Martins
    Date of birth: 14th August 1987

    "He who hesitates is lost."

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    http://en.wikipedia.org/wiki/Sign_extension

    4294967277 = 0xFFFFFFED, 0xED = 237
    4294967234 = 0xFFFFFFC2, 0xC2 = 194

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double's and floats, i need their bytes.
    By Florian in forum C++ Programming
    Replies: 26
    Last Post: 07-08-2008, 05:42 PM
  2. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  3. printing selected bytes of an int variable
    By earth_angel in forum C++ Programming
    Replies: 18
    Last Post: 07-14-2005, 06:23 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

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