C++写的UrlEncode和UrlDecode
来源:互联网 发布:淘宝卖家花呗支付 编辑:程序博客网 时间:2024/05/29 04:23
关于UrlEncode的实现(C++),网上有很多不同的版本,对需要编码的字符集的选取并不统一。那么到底有没有标准呢?答案是有的,参见wiki
绝对不编码的,只有字母、数字、短横线(-)、下划线(_)、点(.)和波浪号(~),其他字符要视情况而定,所以一般性的urlencode只需保留上述字符不进行编码。
下面给出实现:
- unsigned char ToHex(unsigned char x)
- {
- return x > 9 ? x + 55 : x + 48;
- }
- unsigned char FromHex(unsigned char x)
- {
- unsigned char y;
- if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
- else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
- else if (x >= '0' && x <= '9') y = x - '0';
- else assert(0);
- return y;
- }
- std::string UrlEncode(const std::string& str)
- {
- std::string strTemp = "";
- size_t length = str.length();
- for (size_t i = 0; i < length; i++)
- {
- if (isalnum((unsigned char)str[i]) ||
- (str[i] == '-') ||
- (str[i] == '_') ||
- (str[i] == '.') ||
- (str[i] == '~'))
- strTemp += str[i];
- else if (str[i] == ' ')
- strTemp += "+";
- else
- {
- strTemp += '%';
- strTemp += ToHex((unsigned char)str[i] >> 4);
- strTemp += ToHex((unsigned char)str[i] % 16);
- }
- }
- return strTemp;
- }
- std::string UrlDecode(const std::string& str)
- {
- std::string strTemp = "";
- size_t length = str.length();
- for (size_t i = 0; i < length; i++)
- {
- if (str[i] == '+') strTemp += ' ';
- else if (str[i] == '%')
- {
- assert(i + 2 < length);
- unsigned char high = FromHex((unsigned char)str[++i]);
- unsigned char low = FromHex((unsigned char)str[++i]);
- strTemp += high*16 + low;
- }
- else strTemp += str[i];
- }
- return strTemp;
- }
0 0
- C++写的UrlEncode和UrlDecode
- URLEncode和URLDecode的注意事项
- URLDecode 和 URLEncode 在 C 中的实现
- URLDecode 和 URLEncode 在 C 中的实现中的 URLEncode
- 一个urlencode/urldecode的C实现
- Server.UrlEncode和Server.UrlDecode的使用
- Server.UrlEncode和Server.UrlDecode的使用
- .NET UrlEncode 和 UrlDecode 的注意事项
- URLEncode和URLDecode的使用jianji
- URLEncode和URLDecode
- URLEncode和URLDecode
- js urlencode和urldecode
- php urlencode 和 urldecode
- urlencode 和 urldecode
- php urlencode和urldecode
- iOS-URLEncode和URLDecode
- urlEncode&&urldecode的用法
- Server.UrlEncode和Server.UrlDecode
- hibnernate 将sql结果集封装为实体对象
- MySQL必备命令
- HDOJ 题目2486 A simple stone game(K倍动态减法游戏,模板)
- 【Storm总结-2】关于Storm 中Topology的并发度的理解
- 由open()的O_DIRECT参数引发的公共标准的问题
- C++写的UrlEncode和UrlDecode
- 安卓训练-开始-支持不同的设备-支持不同的屏幕
- Java Bitmap操作详解
- activity和fragment生命周期对比
- IOS 文件目录及NSUserDefaults浅谈
- 解决android调试logcat 无 application信息问题
- 使用FREEMARKER的一些总结
- Having problem building jcifs V1.2.19
- 解决Godaddy付款时的There was a problem processing your transaction. Please verify your payment information