Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Forked from sudar/url-encode.c
Created January 22, 2019 21:21
Show Gist options
  • Save jesobreira/4ba48d1699b7527a4a514bfa1d70f61a to your computer and use it in GitHub Desktop.
Save jesobreira/4ba48d1699b7527a4a514bfa1d70f61a to your computer and use it in GitHub Desktop.
URL Encoding in C (urlencode / encodeURIComponent)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* urlencode(char* originalText)
{
// allocate memory for the worst possible case (all characters need to be encoded)
char *encodedText = (char *)malloc(sizeof(char)*strlen(originalText)*3+1);
const char *hex = "0123456789abcdef";
int pos = 0;
for (int i = 0; i < strlen(originalText); i++) {
if (('a' <= originalText[i] && originalText[i] <= 'z')
|| ('A' <= originalText[i] && originalText[i] <= 'Z')
|| ('0' <= originalText[i] && originalText[i] <= '9')) {
encodedText[pos++] = originalText[i];
} else {
encodedText[pos++] = '%';
encodedText[pos++] = hex[originalText[i] >> 4];
encodedText[pos++] = hex[originalText[i] & 15];
}
}
encodedText[pos] = '\0';
return encodedText;
}
int main()
{
printf("%s", urlencode("Hello / world ="));
return 0;
}
@redwinner
Copy link

thanks, short version:
static void urlencode(char * s, char * d)
{
const char *hex = "0123456789ABCDEF";

while(*s)
{
    if (('a' <= *s && *s <= 'z')
        || ('A' <= *s && *s <= 'Z')
        || ('0' <= *s && *s <= '9')) {
            *d++ = *s;
    } else {
        *d++ = '%';
        *d++ = hex[(*s >> 4) & 0x0F];
        *d++ = hex[*s & 0x0F];
    }
    s++;
}
*d = '\0';

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment