#include <stdio.h> #include <stdlib.h> #include <string.h> char ascii_to_char(char src); char* url_decode(const char* src,char* dst,int len); int main(int argc,char* argv[]) { char* src = "%E6%98%A5%E8%8A%82"; int len = strlen(src); char *dst = (char*)malloc(len+1); url_decode(src,dst,len); puts(dst); if(!dst) { free(dst); } return 0; } //将字符形式的ascii码转换成对应的整型的ascii吗 char ascii_to_char(char src) { if(src >= 'A' && src <= 'F') { src-=55; } else if( src >= '0' && src <= '9') { src-=48; } else { return -1; } return src; } char* url_decode(const char* src,char* dst,int len) { //char* temp = (char*)malloc(len+1); //strncpy(temp,src,len+1); int i=0; int j=0; for(;i<len;) { if( src[i] == '%' ) { i++; dst[j]= ( (( ascii_to_char(src[i])) << 4) | ( ascii_to_char(src[i+1]) ) ); j++; i+=2; } else { dst[j]=src[i]; i++; j++; } } dst[j]='\0'; return dst; }
url解码
最新推荐文章于 2021-03-19 15:32:29 发布