C++语言的url encode 和decode
std::string UrlEncode(const std::string& szToEncode)
{
std::string src = szToEncode;
char hex[] = "0123456789ABCDEF";
string dst;
for (size_t i = 0; i < src.size(); ++i)
{
unsigned char cc = src[i];
if ( cc >= 'A' && cc <= 'Z'
|| cc >='a' && cc <= 'z'
|| cc >='0' && cc <= '9'
|| cc == '.'
|| cc == '_'
|| cc == '-'
|| cc == '*')
{
if (cc == ' ')
{
dst += "+";
}
else
dst += cc;
}
else
{
unsigned char c = static_cast<unsigned char>(src[i]);
dst += '%';
dst += hex[c / 16];
dst += hex[c % 16];
}
}
return dst;
}
std::string UrlDecode(const std::string& szToDecode)
{
std::string result;
int hex = 0;
for (size_t i = 0; i < szToDecode.length(); ++i)
{
switch (szToDecode[i])
{
case '+':
result += ' ';
break;
case '%':
if (isxdigit(szToDecode[i + 1]) && isxdigit(szToDecode[i + 2]))
{
std::string hexStr = szToDecode.substr(i + 1, 2);
hex = strtol(hexStr.c_str(), 0, 16);
//字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@]
//可以不经过编码直接用于URL
if (!((hex >= 48 && hex <= 57) || //0-9
(hex >=97 && hex <= 122) || //a-z
(hex >=65 && hex <= 90) || //A-Z
//一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;=?@]
hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29
|| hex == 0x2a || hex == 0x2b|| hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f
|| hex == 0x3A || hex == 0x3B|| hex == 0x3D || hex == 0x3f || hex == 0x40 || hex == 0x5f
))
{
result += char(hex);
i += 2;
}
else result += '%';
}else {
result += '%';
}
break;
default:
result += szToDecode[i];
break;
}
}
return result;
}
C++语言的url encode 和decode的更多相关文章
- [LeetCode] Encode and Decode TinyURL 编码和解码精简URL地址
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
- 用node.js写个在Bash上对字符串进行Base64或URL的encode和decode脚本
一:自己这段时间经常要用到Base64编码和URL编码,写个编译型语言有点麻烦干脆就用node.js弄了个,弄好后在/etc/profile里加上alias就能完成工具的配置,先上代码: functi ...
- 535. Encode and Decode TinyURL 长短URL
[抄题]: TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problem ...
- iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html
iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html 前言 這裡只是講一個故事,一個發生在我身上的真實的故事.曾經,我以為搞加 ...
- 【python】python新手必碰到的问题---encode与decode,中文乱码[转]
转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...
- python encode和decode函数说明【转载】
python encode和decode函数说明 字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在p ...
- python的str,unicode对象的encode和decode方法
python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...
- Encode and Decode TinyURL
TinyURL is a URL shortening service where you enter a URL such as https://leetcode.com/problems/desi ...
- Leetcode: Encode and Decode TinyURL
Note: This is a companion problem to the System Design problem: Design TinyURL. TinyURL is a URL sho ...
随机推荐
- 使用BusyBox制作根文件系统
1.BusyBox简介 BusyBox 是很多标准 Linux 工具的一个单个可执行实现.BusyBox 包含了一些简单的工具,例如 cat 和 echo,还包含了一些更大.更复杂的工具,例如 gre ...
- com.mysql.jdbc.MysqlDataTruncation:Data Truncation:Data too long for column '字段name' at row 1
1.问题描述: 在mysql插入数据的时候报错:Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long fo ...
- MapReduce-实践2
进阶实践4: mapper,reducer输出数据压缩 应用场景 当mapper或者reducer的输出数量比较大,会影响shuffle阶段远程拷贝的网络性能,以及对存储容量的要求:这个时候可以考虑 ...
- Global.asax详解
在网上找了N多相关的东西总说的不够细,现在终于找到了.可以了解web.cofig和Global.asax之间的关系以及执行的顺序. 在Global.asax.cs文件中 protected void ...
- vue-cli Uncaught SyntaxError: Use of const in strict mode解决办法
vue-cli初始化项目,开发环境运行项目使用了webpack-dev-server,而最新版本webpack-dev-server@2.9.1运行项目时,并不能成功的把es6语法转化为es5,所以在 ...
- jQuery 获取Select选择的Text和 Value
jQuery获取Select选择的Text和Value:语法解释: 1. $("#select_id").change(function(){//code...}); / ...
- Opencv+pycharm+anaconda配置
最简便的方法,没有之一 首先下载annconda:(不要从官网下,还要FQ,还要慢) https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 请注 ...
- css3中自定义 placeholder 文本颜色
对于 ie 浏览器我们可以通过自定义的 class 名称,直接修改 span 这个标签的样式.对于其他浏览器诸如谷歌和火狐就需要特殊处理了,不多说直接上代码: ::-webkit-input-plac ...
- 转:CentOS 7使用nmcli配置双网卡聚合LACP
进入CentOS 7以后,网络方面变化比较大,例如eth0不见了,ifconfig不见了,其原因是网络服务全部都由NetworkManager管理了,下面记录下今天下午用nmcli配置的网卡聚合,网络 ...
- C++:栈(stack)的模板类实现
1.基本概念 栈中的元素遵守“先进后出”的原则(LIFO,Last In First Out) 只能在栈顶进行插入和删除操作 压栈(或推入.进栈)即push,将数据放入栈顶并将栈顶指针加一 出栈(或弹 ...