版权声明:本文为博主原创文章,未经博主允许不得转载。有事儿联系博主QQ:1679026015 https://blog.csdn.net/COCO56/article/details/82110376
一:url编码解码简介
url编码解码,又叫百分号编码,是统一资源定位(URL)编码方式。URL地址(常说网址)规定了常用地数字,字母可以直接使用,另外一批作为特殊用户字符也可以直接用(/,:@等),剩下的其它所有字符必须通过%xx编码处理。 现在已经成为一种规范了,基本所有程序语言都有这种编码,如js:有encodeURI、encodeURIComponent,PHP有 urlencode、urldecode等。编码方法很简单,在该字节ascii码的的16进制字符前面加%. 如 空格字符,ascii码是32,对应16进制是'20',那么urlencode编码结果是:%20
二:在线转换工具
网上有很多在线转换工具,可以搜“urlencode” 、“urldecode”
下面这个不错(如无法使用可以搜其他的)。
http://web.chacuo.net/charseturlencode
http://www.atool.org/urlencode.php
一般使用utf8编码,如果不行,也可以试试其他编码方式
三:算法
Python代码:
#!/usr/bin/python
#-*- coding: utf-8 -*-
import urllib.parse
s="http://yz.chsi.com.cn/zsml/querySchAction.do?ssdm=61&dwmc=%E4%B8%AD%E5%9B%BD%E8%88%AA%E5%A4%A9%E7%A7%91%E6%8A%80%E9%9B%86%E5%9B%A2%E5%85%AC%E5%8F%B8%E7%AC%AC%E4%BA%94%E7%A0%94%E7%A9%B6%E9%99%A2%E8%A5%BF%E5%AE%89%E5%88%86%E9%99%A2&mldm=&mlmc=&yjxkdm=0809&xxfs=&zymc="
data = urllib.parse.unquote(s)
print (data)
结果:
下面这个是用C++实现的比较简单的计算,通用性不是太强,可以搜索其他的算法,也可以直接用Python的类库。
URLEncode是这样编码的
1。数字和字母不变。
2。空格变为"+"号。
3。其他被编码成"%"加上他们的ascii的十六进制,规律是这样的
比如“啊”字 Ascii的十六进制是B0A1——>%B0%A1(Note:它是每个字节前加个%)。
*/
#include <iostream>
#include <iostream>
#include <string>
#include <fstream>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
typedef unsigned char BYTE;
inline BYTE toHex(const BYTE &x)
{
return x > 9 ? x + 55 : x + 48;
}
string urlEncoding(string &sIn)
{
cout << "size:" << sIn.size() << endl;
string sOut;
for (int ix = 0; ix < sIn.size(); ix++)
{
BYTE buf[4];
memset(buf, 0, 4);
if (isalnum((BYTE)sIn[ix]))
{
buf[0] = sIn[ix];
}
else if (isspace((BYTE)sIn[ix]))
{
buf[0] = '+';
}
else
{
buf[0] = '%';
buf[1] = toHex((BYTE)sIn[ix] >> 4);
buf[2] = toHex((BYTE)sIn[ix] % 16);
}
sOut += (char *)buf;
}
return sOut;
}
int main(int argc, char *argv[])
{
string src;
cout << "Please input characters." << endl;
cin >> src;
string sOut = urlEncoding(src);
cout << sOut << endl;
system("PAUSE");
return 0;
}