Help with Hexadecimal Conversion

This is a discussion on Help with Hexadecimal Conversion within the C Programming forums, part of the General Programming Boards category; I am trying to create a program where when I enter a character, after the user presses enter it displays ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Help with Hexadecimal Conversion

    I am trying to create a program where when I enter a character, after the user presses enter it displays the hexadecimal of it. I am seeming to have problems finding how to do it. Any suggestions? Thanks

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    324
    Something like this?

    Code:
    #include <stdio.h>
    
    int main()
    {
        char c = 10;
        printf("%x\n", c);
        return 0;
    }
    That's the easy way. If you want to write a function like toHex() that's a whole different ballgame.

    edit: maybe you are having problems with the input as well?

    Code:
    char c;
    scanf("%c", &c);

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    reply

    Is that what the %x does?? Converts it into a hexadecimal. Here is what I have so far..
    Code:
    #include <stdio.h>
    
    main()
    {       char c[5]; 
                               
    puts("Enter Character:");
            while(gets(c) != NULL)   
            {        puts(c);        
                     printf("\n");     
            }
    }

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    %x is just another format specifier like %d, %f etc.it is used for hexadecimal numbers.it gives the hexadecimal form of the corresponding argument.
    for eg.
    Code:
    int i=15;
    printf("%x",i);
    will output F
    Similarly %o is used for octal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to hexadecimal conversion
    By Nathalie in forum C Programming
    Replies: 9
    Last Post: 12-11-2007, 02:29 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  5. hexadecimal conversion question
    By KnightKap in forum C++ Programming
    Replies: 3
    Last Post: 09-18-2001, 11:49 PM

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