printf hex

This is a discussion on printf hex within the C Programming forums, part of the General Programming Boards category; Hey guys just wondering how i can print a hexadecimal using printf...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    printf hex

    Hey guys just wondering how i can print a hexadecimal using printf

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    Use the %x modifier.

  3. #3
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
    /* har har */
    int a = 0x09; /* 9 (hex) */
    printf("%d", a); /* outputs 9 (hex) */
    Next time, why don't you read some documentation before posting such a question?

    http://www.google.com/search?q=printf+hexadecimal

  4. #4
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    You can try %X also. Try it too...
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  5. #5
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    As with all printf() formatting, you use the correct format template specifier. In the case of hexadecimal, you can use either "%x" or "%X", depending on whether you want the alpha digits to be upper or lower case. To get a particular width, with zero padding on the left, you use something like "%08x" (assuming that you want to print the value as an 8 digit hexadecimal number).

    For example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
        long octval;
        char *octend;
        long decval;
        char *decend;
        long hexval;
        char *hexend;
    
        if (argc == 2)
        {
            octval = strtol(argv[1], &octend, 8); /* read as octal */
            decval = strtol(argv[1], &decend, 10); /* read as decimal */
            hexval = strtol(argv[1], &hexend, 16); /* read as hexadecimal */
    
            printf("The string '%s' can be interpreted thus:\n", argv[1]);
            printf("   Oct: %011o (dec %d, hex %08x, %d chars)\n",
                     octval, octval, octval, (int)(octend - argv[1]));
            printf("   Dec: %d (oct %011o, hex %08x, %d chars)\n",
                     decval, decval, decval, (int)(decend - argv[1]));
            printf("   Hex: %08x (oct %011o, dec %d, %d chars)\n",
                     hexval, hexval, hexval, (int)(hexend - argv[1]));
        }
        else
        {
            printf("Usage: %s valuestring\n", argv[0]);
        }
        return 0;
    }
    Insert obnoxious but pithy remark here

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    22
    Quote Originally Posted by cwr
    Code:
    /* har har */
    int a = 0x09; /* 9 (hex) */
    printf("%d", a); /* outputs 9 (hex) */
    I think your reply is a bit confusing.

    Code:
    int a = 0x0a;
    printf("%d",a);  /* outputs 10 (decimal)
    printf("0x%x",a); /* outputs 0xa */
    I think the original OP is looking how to print out the hex value, as shown by other posters you can use the %x modifier.
    Last edited by slcjoey; 01-04-2006 at 12:15 PM.

  7. #7
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    Quote Originally Posted by slcjoey
    I think your reply is a bit confusing.

    Code:
    int a = 0x0a;
    printf("%d",a);  /* outputs 10 (decimal)
    printf("0x%x",a); /* outputs 0xa */
    I think the original OP is looking how to print out the hex value, as shown by other posters you can use the %x modifier.

    duh. lol
    did you notice the "har har" comment?
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  4. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

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