Skip to content

Instantly share code, notes, and snippets.

@masterzorag
Created December 17, 2015 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masterzorag/4203f911cb43ce0ebb99 to your computer and use it in GitHub Desktop.
Save masterzorag/4203f911cb43ce0ebb99 to your computer and use it in GitHub Desktop.
C URL encoding
/*
https://gist.githubusercontent.com/sudar/3404970/raw/3f085b750aaa2ff2f49b148827a19915a4857222/url-encode.c
gcc -o test urlenc.c -Wall
./test "[a b]"
%5ba%20b%5d
*/
#include <stdio.h>
int main (int argc, char *argv[])
{
char *hex = "0123456789abcdef", *c = argv[1];
while(*c != '\0')
{
if(('a' <= *c && *c <= 'z')
|| ('A' <= *c && *c <= 'Z')
|| ('0' <= *c && *c <= '9'))
{
putchar(*c);
}
else
{
putchar('%');
putchar(hex[*c >> 4]);
putchar(hex[*c & 15]);
}
c++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment