Thread: Printing integers in hex

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    3

    Printing integers in hex

    Hello.
    I am currently reteaching myself C (early stages of learning yet) and I am having an issue with printing the values of an integer in Hex. In the code below, it works correctly until i get to 128 then it decides to print a few f's in front of the the first byte of the hex.

    Example print out should look like
    FF 00 00 00
    but it prints
    FFFFFFFF 00 00 00.

    Is there an issue in my pointer? it tells me there is a bad ptr when I debug it, but i don't know what is bad about it. Here is my code. Any tips would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int i = 0;
    
    void print_hex_signed_unsigned (int i)
    {
     char*
    	 hex_ptr = (char*)&i;
     printf("%02x ", hex_ptr[0]);
     printf("%02x ", hex_ptr[1]);
     printf("%02x ", hex_ptr[2]);
     printf("%02x\t", hex_ptr[3]);
     printf("%14d\t", i);
     printf("%14u\n", i);
    }
    
    int main ()
    {
    int index;
        for (index = 0; index < 256; index++)
            print_hex_signed_unsigned(index * 1);
    	printf("press enter to continue");
        getchar();
    return(0);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This assignment seems familiar, somehow. Anyway, as a char, 128 is actually -128. Since it gets promoted to an int when passed to printf, it is copied by value (so the sign extends with all those 1s, which look like f's when printed in hex). If you don't want that, try (unsigned char *) instead.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Is there a reason you don't just do one printf() call?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by tabstop View Post
    This assignment seems familiar, somehow. Anyway, as a char, 128 is actually -128. Since it gets promoted to an int when passed to printf, it is copied by value (so the sign extends with all those 1s, which look like f's when printed in hex). If you don't want that, try (unsigned char *) instead.
    Thanks. That worked perfectly. I think i half tried that before, but forgot to make both unsigned.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    Quote Originally Posted by master5001 View Post
    Is there a reason you don't just do one printf() call?
    Not really. Just helps me visualize the output a little better.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fair 'nuff. I once got to thinking about how cool it would be if the optimizer could optimize code by making all encompassing *printf() passes. I guess manual optimizations will always have their place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printing hex
    By axr0284 in forum C Programming
    Replies: 2
    Last Post: 04-28-2006, 06:29 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Printing Hex Value of a char
    By earth_angel in forum C Programming
    Replies: 6
    Last Post: 05-25-2005, 10:49 AM
  4. Printing numbers in hex format
    By miclus in forum C++ Programming
    Replies: 7
    Last Post: 01-29-2005, 07:04 AM
  5. Printing Hex as Binary
    By Mystic_Skies in forum C Programming
    Replies: 6
    Last Post: 11-22-2004, 04:18 PM