这里的内容是处理%xx之类的东西!是"解码"过程。
比如网站请求服务器的某些资源带有中文,或者linux系统某些文件中记录的文件名中含有中文都会变为诸如%xx的,可以对其“解码”获取中文
void strdecode(char *to, char *from)
{
for ( ; *from != '\0'; ++to, ++from)
{
if (from[0] == '%' && isxdigit(from[1]) && isxdigit(from[2]))
{
*to = hexit(from[1])*16 + hexit(from[2]);
from += 2;
}
else
*to = *from;
}
*to = '\0';
}
int hexit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return 0;
}
//“编码”,用作回写浏览器的时候,将除字母数字及/_.-~以外的字符转义后回写。
//strencode(encoded_name, sizeof(encoded_name), name);
void strencode(char* to, size_t tosize, const char* from)
{
int tolen;
for (tolen = 0; *from != '\0' && tolen + 4 < tosize; ++from)
{
if (isalnum(*from) || strchr("/_.-~", *from) != (char*)0)
{
*to = *from;
++to;
++tolen;
}
else
{
sprintf(to, "%%%02x", (int) *from & 0xff);
to += 3;
tolen += 3;
}
}
*to = '\0';
}
例如如下:/home/uos/.local/share/recently-used.xbel这个文件中的一部分内容(这个记录了双击打开过的文件记录)
而/home/uos/Desktop/%E6%88%AA%E5%9B%BE%E5%BD%95%E5%B1%8F_20201204111933.png进行URL解码后为/home/uos/Desktop/截图录屏_20201204111933.png