轉載自:http://blog.csdn.net/langeldep/article/details/6264058
本文代碼為從PHP代碼中修改而來,只保留了2個函數。
int php_url_decode(char *str, int len);
char *php_url_encode(char const *s, int len, int *new_length);
URL編碼做了如下操作:
字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_" 都不被編碼,維持原值;
空格" "被轉換為加號"+"。
其他每個字節都被表示成"%xy"格式的由3個字符組成的字符串,編碼為UTF-8。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <sys/types.h> static unsigned char hexchars[] = "0123456789ABCDEF"; /**
* 16進制數轉換成10進制數
* 如:0xE4=14*16+4=228
*/ static int php_htoi(char *s) { int value; int c; c = ((unsigned char *)s)[0]; if (isupper(c)) c = tolower(c); value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16; c = ((unsigned char *)s)[1]; if (isupper(c)) c = tolower(c); value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10; return (value); } char *php_url_encode(char const *s, int len, int *new_length) { register unsigned char c; unsigned char *to, *start; unsigned char const *from, *end; from = (unsigned char *)s; end = (unsigned char *)s + len; start = to = (unsigned char *) calloc(1, 3*len+1); while (from < end) { c = *from++; if (c == ' ') { *to++ = '+'; } else if ((c < '0' && c != '-' && c != '.') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a' && c != '_') || (c > 'z')) { to[0] = '%'; to[1] = hexchars[c >> 4];//將2進制轉換成16進制表示 to[2] = hexchars[c & 15];//將2進制轉換成16進制表示 to += 3; } else { *to++ = c; } } *to = 0; if (new_length) { *new_length = to - start; } return (char *) start; } int php_url_decode(char *str, int len) { char *dest = str; char *data = str; while (len--) { if (*data == '+') { *dest = ' '; } else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) && isxdigit((int) *(data + 2))) { *dest = (char) php_htoi(data + 1); data += 2; len -= 2; } else { *dest = *data; } data++; dest++; } *dest = '\0'; return dest - str; } int main(int argc, char ** argv) { char *str = "你好,世界!",*new_str; int len = 0,new_len = 0,old_len = 0; len = strlen(str); new_str = php_url_encode(str,len,&new_len); printf("new string : %s,new length : %d\n",new_str,new_len); old_len = php_url_decode(new_str,new_len); printf("old string : %s,old length : %d\n",new_str,old_len); return 0; }
ps:查了好多資料才弄明白urldecode的原理,原來如此簡單,呵呵