std::string UrlEncode(const std::string& szToEncode)
{
std::string src = szToEncode;
char hex[] = "0123456789ABCDEF";
string dst; for (size_t i = ; i < src.size(); ++i)
{
unsigned char cc = src[i];
if (isascii(cc))
{
if (cc == ' ')
{
dst += "%20";
}
else
dst += cc;
}
else
{
unsigned char c = static_cast<unsigned char>(src[i]);
dst += '%';
dst += hex[c / ];
dst += hex[c % ];
}
}
return dst;
} std::string UrlDecode(const std::string& szToDecode)
{
std::string result;
int hex = ;
for (size_t i = ; i < szToDecode.length(); ++i)
{
switch (szToDecode[i])
{
case '+':
result += ' ';
break;
case '%':
if (isxdigit(szToDecode[i + ]) && isxdigit(szToDecode[i + ]))
{
std::string hexStr = szToDecode.substr(i + , );
hex = strtol(hexStr.c_str(), , );
//字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@]
//可以不经过编码直接用于URL
if (!((hex >= && hex <= ) || //0-9
(hex >= && hex <= ) || //a-z
(hex >= && hex <= ) || //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 += ;
}
else result += '%';
}else {
result += '%';
}
break;
default:
result += szToDecode[i];
break;
}
}
return result;
}

在线测试工具:http://tool.chinaz.com/Tools/URLEncode.aspx

c++ encode decode的更多相关文章

  1. node_nibbler:自定义Base32/base64 encode/decode库

    https://github.com/mattrobenolt/node_nibbler 可以将本源码复制到自己需要的JS文件中,比如下面这个文件,一个基于BASE64加密请求参数的REST工具: [ ...

  2. python中文处理之encode/decode函数

    python中文处理相信迷惑过不少同学.下面说说python2/3的encode和decode函数. python2中,使用decode()和encode()来进行解码和编码,以unicode类型作为 ...

  3. python编码问题之\"encode\"&\"decode\"

    python encode decode 编码 decode的作用是将其他编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换 ...

  4. python编码encode decode(解惑)

    关于python 字符串编码一直没有搞清楚,今天总结了一下. Python 字符串类型 Python有两种字符串类型:str 与 unicode. 字符串实例 # -*- coding: utf-8 ...

  5. (转)Integrating Intel® Media SDK with FFmpeg for mux/demuxing and audio encode/decode usages 1

    Download Article and Source Code Download Integrating Intel® Media SDK with FFmpeg for mux/demuxing ...

  6. python3.3 unicode(encode&decode)

    最近在用python写多语言的一个插件时,涉及到python3.x中的unicode和编码操作,本文就是针对编码问题研究的汇总,目前已开源至github.以下内容来自项目中的README. 1 ASC ...

  7. 【学习笔记】--- 老男孩学Python,day7 python中is 和 == 的区别 encode decode

    is比较的是id(内存地址)是不是一样,==比较的是值是不是一样 Python中,万物皆对象!万物皆对象!万物皆对象!(很重要,重复3遍) 每个对象包含3个属性,id,type,value id就是对 ...

  8. javascript base64 encode decode 支持中文

    * 字符编码 ** 一定要知道数据的字符编码 ** 使用utf-8字符编码存储数据 ** 使用utf-8字符编码输出数据 * Crypto.js 支持中文 Base64编码说明 Base64编码要求把 ...

  9. 字符编码和python .encode().decode()方法

    字符编码与encode.decode的问题:  用8个开关表示世界万物   ASCII :  American Standard Code for Information Interchange,美国 ...

  10. python encode decode unicode区别及用法

    decode 解码 encode 转码 unicode是一种编码,具体可以百度搜 # coding: UTF-8 u = u'汉' print repr(u) # u'\u6c49' s = u.en ...

随机推荐

  1. qml json 解析到 ListView

    https://github.com/kromain/qml-utils/tree/master/JSONListModel 非常棒!! 实现的原理如下文: http://goessner.net/a ...

  2. android应用程序如何调用支付宝接口(转)

    最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...

  3. Mongodb Management Studio

    1.服务器管理功能添加服务器,删除服务器 2.服务器,数据库,表,列,索引,树形显示和状态信息查看 3.查询分析器功能.支持select,insert,Delete,update支持自定义分页函数 $ ...

  4. 动态加载的数据,hover效果

    $('.option li').live('mouseenter',function(){            $(this).addClass("active");       ...

  5. POJ 2632 Crashing Robots 模拟 难度:0

    http://poj.org/problem?id=2632 #include<cstdio> #include <cstring> #include <algorith ...

  6. HDU 4036 存疑题目,数论 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4036 一开始以为需要用斜抛,结果发现只需要用能量守恒定律?+与最大速度的坏土豆速度保持一致 #include & ...

  7. javascript photo1

  8. C-union的使用

    union有两个作用: 1,节约空间,如果一个struct存在两个互斥的变量,则可以把这个struct变成union 2,将同一个内存作为多种解释 代码: #include <iostream& ...

  9. Hibernate中的组合映射

    1.实体bean设计 car: public class Car { private int id; private String name; private Wheel wheel; set... ...

  10. 戴文的Linux内核专题:08内核配置(4)

    转自Linux中国 这个第四部分里,我们将继续配置更多的设置和特性. 这里我们被问及关于"IBM Calgary IOMMU support (CALGARY_IOMMU)".这个 ...