DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Decode An Encoded Query String In C

07.30.2005
| 6654 views |
  • submit to reddit
        Here's a little function to decode part of a query string that has been encoded as per the HTML specification with %xx notation, where xx is the hexidecimal representation of a character.

Be warned that this function modifies the contents of the string you pass as the argument!

#include <stdlib.h>

char* decode_query (char* str)
{
        char*           in = str;
        char*           out = str;

        char            c = 0;
        char            decode_buffer[5] = { '0', 'x', 0, 0, 0 };

        while ((c = *in++)) {
                if (c == '%' && *in && *(in + 1)) {
                        decode_buffer[2] = *in++;
                        decode_buffer[3] = *in++;

                        c = char(strtod(decode_buffer, (char**) NULL));
                } else if (c == '+')
                        c = ' ';

                *out++ = c;
        }

        *out = 0;

        return str;
}