测试环境:freebsd
补上urldecode
#include <stdio.h> #include <sys/types.h> #include <regex.h> #define safe_emalloc(nmemb, size, offset) malloc((nmemb) * (size) + (offset)) char *test( char * src); char *urlencode( char *in_str ); char *php_url_encode(char *s , int len , int *new_length); int php_url_decode(char *str); static int php_htoi(char *s); static char hexchars[] = "0123456789abcdef"; static char HEXCHARS[] = "0123456789ABCDEF"; /* 主程序 */ int main() { char *p; char *d; int di; char s[10]="ehd你好"; p=urlencode(s); printf( " %s \n" , p ); di=php_url_decode(p); printf( " %s \n" , p ); char fee[10]="000500"; int i,n; n=strlen(fee); for (i=0;i<n ;i++ ) { if(fee[i]!='0') { break; } } printf( "%s \n" , &fee[i] ); } char *urlencode( char *in_str ) { char *out_str; int in_str_len, out_str_len; in_str_len=strlen(in_str); out_str_len=0; out_str = php_url_encode( in_str , in_str_len, &out_str_len); return (char *) out_str; } char *php_url_encode(char *s , int len , int *new_length) { register unsigned char c; unsigned char *to, *start; unsigned char const *from, *end; from = s; end = s + len; start = to = (unsigned char *) safe_emalloc(3, len, 1); while (from < end) { c = *from++; //printf("%d\n" , c); if (c == ' ') { *to++ = '+'; #ifndef CHARSET_EBCDIC } else if ((c < '0' && c != '-' && c != '.') || (c < 'A' && c > '9') || (c > 'Z' && c < 'a' && c != '_') || (c > 'z')) { to[0] = '%'; to[1] = hexchars[c >> 4]; to[2] = hexchars[c & 15]; to += 3; #else /*CHARSET_EBCDIC*/ } else if (!isalnum(c) && strchr("_-.", c) == NULL) { /* Allow only alphanumeric chars and '_', '-', '.'; escape the rest */ to[0] = '%'; to[1] = hexchars[os_toascii[c] >> 4]; to[2] = hexchars[os_toascii[c] & 15]; to += 3; #endif /*CHARSET_EBCDIC*/ } 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; len=strlen(str); while (len--) { if (*data == '+') { *dest = ' '; } else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1)) && isxdigit((int) *(data + 2))) { #ifndef CHARSET_EBCDIC *dest = (char) php_htoi(data + 1); #else *dest = os_toebcdic[(char) php_htoi(data + 1)]; #endif data += 2; len -= 2; } else { *dest = *data; } data++; dest++; } *dest = '\0'; return dest - str; } 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); } /* gcc test.c -o test ./test */

调试一直这个错误
conflicting types
错误:
test.c:22: error: conflicting types for 'urlencode'
test.c:18: error: previous implicit declaration of 'urlencode' was here


原来是因为没有先做函数声明,而函数位于main()之后。
在main函数前声明了函数原型后,一切ok.