Thread: Ascii to Hex

  1. #1
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122

    Ascii to Hex

    Is there a function available in C to easily lookup the hex value for an ascii char? I.e. If I wanted to encode so called 'special' chars to their hex values as with URLs;

    so: "hello world" becomes "hello%20world"

    or would I have to create my own switch-case statement for checking current char and print it to a string such as:

    Code:
    ...
    /* loop through string, until at end of string */
    /* code to read current char in string */
    ...
    switch currentChar{
      case ' ' : sprintf(str, "%20", null); break;
      case '%' : sprintf(str, "%25", null); break;
      ...
      default: return 1; /* ERROR! - should never be! */
    }
    or maybe even a few iteration statements to find the special chars that require encoding!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like
    sprintf( str, "%%%02x", currentChar );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you need a % in a sprintf, then you need to use %%, as % is a formatting prefix.

    But if you wan't an ASCII to Hex conversion, something like this would do:

    Code:
    void asctohex(char a, char *s) 
    {
         char c;
         c = (a >> 4) & 0x0f;
         if (c <= 9) c+= '0'; else c += 'a' - 10;
         *s++ = c;
         c = a & 0x0f;
         if (c <= 9) c+= '0'; else c += 'a' - 10;
         *s++ = c;
         *s = 0;
    }
    Alternatively, if you fancy using sprintf, use something like:

    Code:
        switch(currentChar) {
           case ' ':
           case '%':
           ...
              sprintf(str, "%%%2x", currentChar);
              break;
           default:
              break;
        }
    --
    Mats

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't look them up...
    Convert decimal (ie the chars position in the ascii table) to hex and ta dar.

  5. #5
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Quote Originally Posted by zacs7 View Post
    Don't look them up...
    Convert decimal (ie the chars position in the ascii table) to hex and ta dar.
    How do I find the chars position in the ASCII table? i.e. how to find that char 'a' is denery value 97 and hex value 61

    thanks.
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    The following will convert any string into a string of hexvalues:
    Code:
    char * conv_to_hex (char * input, int max_len) {
        int ii = 0;
        char * out_string = (char *) malloc0 (sizeof (char) * ((max_len*2)+1));
        
        for (ii = 0; ii < max_len; ii++) {
            sprintf ( (out_string+(ii*2)), "&#37;02x", (unsigned char) (*(input+ii)));
        }
    
        out_string[(max_len*2)] = '\0';
    
        return out_string;
    }
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bobthebullet990 View Post
    How do I find the chars position in the ASCII table? i.e. how to find that char 'a' is denery value 97 and hex value 61

    thanks.
    if you do printf("%d", 'a'); then you'll get 97, replace %d with %x and you'll get 61 - chars are already their ASCII value (unless you are running on really old IBM hardware that uses the EBCDIC character set of course - but I doubt those are very common these days, and you probably would have said if that's what your system was).

    --
    Mats

  8. #8
    Matt Conway bobthebullet990's Avatar
    Join Date
    Nov 2005
    Location
    Cambridge
    Posts
    122
    Nice one! thanks guys! ...and just one more quick question...

    If i can do printf("&#37;d", 'a'); to print the denery and printf("%x", 'a'); to print the hex, is it then possible to do:

    sprintf(str, "%d", 'a'); and sprintf(str, "%x",'a'); and the same effect will occur?

    Thanks!
    Many junglists take pride in their belongin to what may be referred to as a globalised drum & bass subculture, as a subculture though, it is not nearly as distinct at gothic or punk!

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    yeah, but you'll need a string to print *into*
    Code:
    sprintf("&#37;d", 'a'); /* Won't compile */
    sprintf (out_string, "%d", 'a'); /* Will compile */
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char to ASCII Hex Value Troubles
    By MattMik in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2008, 02:17 PM
  2. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Encrypting text file with ASCII hex
    By supaben34 in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2005, 06:35 PM
  5. Hex to Ascii conversion
    By Ryno in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 09:16 AM