转载:http://www.cnblogs.com/newcj/p/3645749.html

 #include "UrlEncode.h"
#include <string>
#include <windows.h>
#include <assert.h> using namespace std; namespace ConnectSDK
{
namespace Utility
{
const wchar_t * hexenc[] = {
L"%00", L"%01", L"%02", L"%03", L"%04", L"%05", L"%06", L"%07",
L"%08", L"%09", L"%0a", L"%0b", L"%0c", L"%0d", L"%0e", L"%0f",
L"%10", L"%11", L"%12", L"%13", L"%14", L"%15", L"%16", L"%17",
L"%18", L"%19", L"%1a", L"%1b", L"%1c", L"%1d", L"%1e", L"%1f",
L"%20", L"%21", L"%22", L"%23", L"%24", L"%25", L"%26", L"%27",
L"%28", L"%29", L"%2a", L"%2b", L"%2c", L"%2d", L"%2e", L"%2f",
L"%30", L"%31", L"%32", L"%33", L"%34", L"%35", L"%36", L"%37",
L"%38", L"%39", L"%3a", L"%3b", L"%3c", L"%3d", L"%3e", L"%3f",
L"%40", L"%41", L"%42", L"%43", L"%44", L"%45", L"%46", L"%47",
L"%48", L"%49", L"%4a", L"%4b", L"%4c", L"%4d", L"%4e", L"%4f",
L"%50", L"%51", L"%52", L"%53", L"%54", L"%55", L"%56", L"%57",
L"%58", L"%59", L"%5a", L"%5b", L"%5c", L"%5d", L"%5e", L"%5f",
L"%60", L"%61", L"%62", L"%63", L"%64", L"%65", L"%66", L"%67",
L"%68", L"%69", L"%6a", L"%6b", L"%6c", L"%6d", L"%6e", L"%6f",
L"%70", L"%71", L"%72", L"%73", L"%74", L"%75", L"%76", L"%77",
L"%78", L"%79", L"%7a", L"%7b", L"%7c", L"%7d", L"%7e", L"%7f",
L"%80", L"%81", L"%82", L"%83", L"%84", L"%85", L"%86", L"%87",
L"%88", L"%89", L"%8a", L"%8b", L"%8c", L"%8d", L"%8e", L"%8f",
L"%90", L"%91", L"%92", L"%93", L"%94", L"%95", L"%96", L"%97",
L"%98", L"%99", L"%9a", L"%9b", L"%9c", L"%9d", L"%9e", L"%9f",
L"%a0", L"%a1", L"%a2", L"%a3", L"%a4", L"%a5", L"%a6", L"%a7",
L"%a8", L"%a9", L"%aa", L"%ab", L"%ac", L"%ad", L"%ae", L"%af",
L"%b0", L"%b1", L"%b2", L"%b3", L"%b4", L"%b5", L"%b6", L"%b7",
L"%b8", L"%b9", L"%ba", L"%bb", L"%bc", L"%bd", L"%be", L"%bf",
L"%c0", L"%c1", L"%c2", L"%c3", L"%c4", L"%c5", L"%c6", L"%c7",
L"%c8", L"%c9", L"%ca", L"%cb", L"%cc", L"%cd", L"%ce", L"%cf",
L"%d0", L"%d1", L"%d2", L"%d3", L"%d4", L"%d5", L"%d6", L"%d7",
L"%d8", L"%d9", L"%da", L"%db", L"%dc", L"%dd", L"%de", L"%df",
L"%e0", L"%e1", L"%e2", L"%e3", L"%e4", L"%e5", L"%e6", L"%e7",
L"%e8", L"%e9", L"%ea", L"%eb", L"%ec", L"%ed", L"%ee", L"%ef",
L"%f0", L"%f1", L"%f2", L"%f3", L"%f4", L"%f5", L"%f6", L"%f7",
L"%f8", L"%f9", L"%fa", L"%fb", L"%fc", L"%fd", L"%fe", L"%ff"
}; String^ UrlEncode(String^ url)
{
wstring text(url->Data()); size_t len = text.length();
std::wstring encoded = L"";
for(size_t i = ; i < len; i++)
{
wchar_t wch = text.at(i);
if ('A' <= wch && wch <= 'Z') {
encoded += wch;
} else if ('a' <= wch && wch <= 'z') {
encoded += wch;
} else if ('' <= wch && wch <= '') {
encoded += wch;
} else if (wch == ' ') {
encoded += hexenc[wch];
} else if (wch == '-' || wch == '_'
|| wch == '.' || wch == '!'
|| wch == '~' || wch == '*'
|| wch == '\'' || wch == '('
|| wch == ')') {
encoded += hexenc[wch];
} else if (wch <= 0x007f) { // other ASCII
encoded += hexenc[wch];
} else if (wch <= 0x07FF) { // non-ASCII <= 0x7FF
encoded += hexenc[0xc0 | (wch >> )];
encoded += hexenc[0x80 | (wch & 0x3F)];
} else { // 0x7FF < ch <= 0xFFFF
encoded += hexenc[0xe0 | (wch >> )];
encoded += hexenc[0x80 | ((wch >> ) & 0x3F)];
encoded += hexenc[0x80 | (wch & 0x3F)];
}
}
return ref new String(encoded.c_str());
} String^ UrlDecode(String^ encodeUrl)
{
wstring text(encodeUrl->Data());
std::wstring decoded = L"";
wchar_t temp[] = L"0x00";
size_t len = text.length();
int sequence = ;
wchar_t conwch = ;
for(size_t i = ; i < len; i++)
{
wchar_t wch = text.at(i++);
if((wch == '%') && (i+ < len))
{
temp[] = text.at(i++);
temp[] = text.at(i);
long tconwch = wcstol(temp, NULL, );
if(tconwch <= 0x7F) {
decoded += tconwch; // normal ascii char
} else if(tconwch >= 0x80 && tconwch <= 0xBF) { // partial byte
tconwch = tconwch & 0x3F;
if(sequence-- == )
tconwch = tconwch << ;
conwch |= tconwch;
if(sequence == )
decoded += conwch;
} else if(tconwch >= 0xC0 && tconwch <= 0xDF) {
conwch = (tconwch & 0x1F) << ; // make space for partial bytes
sequence = ; // 1 more partial bytes follow
} else if(tconwch >= 0xE0 && tconwch <= 0xEF) {
conwch = (tconwch & 0xF) << ; // make space for partial bytes
sequence = ; // 2 more partial bytes follow
} // TODO add case fore 3 partial bytes ... very rare
} else {
decoded += text.at(--i);
}
}
return ref new String(decoded.c_str());
}
}
}

上面是网上找的:

根据项目需要进行修改:

UrlEncode:

 CString URLEncode(CString url)
{
std::wstring text = url; size_t len = text.length();
std::wstring encoded = L"";
for(size_t i = ; i < len; i++)
{
wchar_t wch = text.at(i);
if ('A' <= wch && wch <= 'Z') {
encoded += wch;
} else if ('a' <= wch && wch <= 'z') {
encoded += wch;
} else if ('' <= wch && wch <= '') {
encoded += wch;
} else if (wch == ' ') {
encoded += hexenc[wch];
} else if (wch == '-' || wch == '_'
|| wch == '.' || wch == '!'
|| wch == '~' || wch == '*'
|| wch == '\'' || wch == '('
|| wch == ')') {
encoded += hexenc[wch];
} else if (wch <= 0x007f) { // other ASCII
encoded += hexenc[wch];
} else if (wch <= 0x07FF) { // non-ASCII <= 0x7FF
encoded += hexenc[0xc0 | (wch >> )];
encoded += hexenc[0x80 | (wch & 0x3F)];
} else { // 0x7FF < ch <= 0xFFFF
encoded += hexenc[0xe0 | (wch >> )];
encoded += hexenc[0x80 | ((wch >> ) & 0x3F)];
encoded += hexenc[0x80 | (wch & 0x3F)];
}
}
return encoded.c_str();
}
UrlDecode:
    CString UrlDecode(CString encodeUrl)
{
std::wstring text = encodeUrl;
std::wstring decoded = L"";
wchar_t temp[] = L"0x00";
size_t len = text.length();
int sequence = ;
wchar_t conwch = ;
for(size_t i = ; i < len; i++)
{
wchar_t wch = text.at(i++);
if((wch == '%') && (i+ < len))
{
temp[] = text.at(i++);
temp[] = text.at(i);
long tconwch = wcstol(temp, NULL, );
if(tconwch <= 0x7F) {
decoded += tconwch; // normal ascii char
} else if(tconwch >= 0x80 && tconwch <= 0xBF) { // partial byte
tconwch = tconwch & 0x3F;
if(sequence-- == )
tconwch = tconwch << ;
conwch |= tconwch;
if(sequence == )
decoded += conwch;
} else if(tconwch >= 0xC0 && tconwch <= 0xDF) {
conwch = (tconwch & 0x1F) << ; // make space for partial bytes
sequence = ; // 1 more partial bytes follow
} else if(tconwch >= 0xE0 && tconwch <= 0xEF) {
conwch = (tconwch & 0xF) << ; // make space for partial bytes
sequence = ; // 2 more partial bytes follow
} // TODO add case fore 3 partial bytes ... very rare
} else {
decoded += text.at(--i);
}
}
return decoded.c_str();
}

C++ UTF8 UrlEncode(宽字符)(转载)的更多相关文章

  1. GBK转utf-8,宽字符转窄字符

    //GBK转UTF8 string CAppString::GBKToUTF8(const string & strGBK) { string strOutUTF8 = "" ...

  2. 彻底弄懂UTF-8、Unicode、宽字符、locale

    目录 Unicode.UCS UTF8 宽字符类型wchar_t locale 为什么需要宽字符类型 多字节字符串和宽字符串相互转换 最近使用到了wchar_t类型,所以准备详细探究下,没想到水还挺深 ...

  3. C++ UTF8 UrlEncode(宽字符)

    为了支持C++ UrlEncode之后的字符串能够被C#所识别(windows phone 下C#只能支持UTF8与 Unicode). 所谓的 UTF8 UrlEncode 也只是宽字符串 UrlE ...

  4. 宽字符、多字节、unicode、utf-8、gbk编码转化

    今天遇到一个编码的问题,困惑了我很长时间,所以就简要的的了解了一下常用的编码类型. 我们最常见的是assic编码,它是一种单字节编码,对多容纳256个字符. 我们在编程的时候经常遇到unicode,u ...

  5. [转帖]彻底弄懂UTF-8、Unicode、宽字符、locale

    彻底弄懂UTF-8.Unicode.宽字符.locale linux后端开发   已关注   彻底弄懂UTF-.Unicode.宽字符.locale unicode 是字符集 utf-8是编码格式.. ...

  6. SQL注入之Sqli-labs系列第三十二关(基于宽字符逃逸注入)

    开始挑战第三十二关(Bypass addslashes) 0x1查看源代码 (1)代码关键点 很明显,代码中利用正则匹配将 [ /,'," ]这些三个符号都过滤掉了 function che ...

  7. Unicode,GBK,GB2312,UTF-8概念基础(转载)

    第一篇:JAVA字符编码系列一:Unicode,GBK,GB2312,UTF-8概念基础本部分采用重用,转载一篇文章来完成这部分的目标.来源:holen'blog   对字符编码与Unicode,IS ...

  8. gcc编译器对宽字符的识别

    最早是使用VC++工具来学习C++,学的越多就越对VC挡住的我看不见的东西好奇,总想多接触一些开发环境,今日抽空摸索了一下CodeBlocks这个开源的IDE使用方法,配置的编译器是MinGW的gcc ...

  9. Android NDK 下的宽字符编码转换及icu库的使用(转)

    原贴http://topic.csdn.net/u/20101022/16/1b2e0cec-b9d2-42ea-8d9c-4f1bb8320a54.html?r=70149216 ,看过并动手实现, ...

随机推荐

  1. @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不

    @有两个含义:1,在参数里,以表明该变量为伪参数 ,在本例中下文里将用@name变量代入当前代码中 2,在字串中,@的意思就是后面的字串以它原本的含义显示,如果不加@那么需要用一些转义符\来显示一些特 ...

  2. 关于设置oracle中系统编号SYSID自动编号的问题;

    http://liye9801.blog.163.com/blog/static/601970320086210039591/ 如何在oracle里设置自动编号列 2008-07-21 12:00:3 ...

  3. java 中多线程和锁的使用

    关键词: implements  实现  Runnable 类 run()  方法 注意点 : 创建类的实例 InterfaceController inter=new InterfaceContro ...

  4. java 中 equals和==的区别

    public static void main(String[] args) { int n=0; int m=0; System.out.println(n==m); String str = ne ...

  5. jvm排查工具

    jps jps -mvl --查看java的ps进程. jstack 打印一个线程堆栈信息 top -H -p pid1 -> 得到占用资源大的pid2 jstack pid1 | grep & ...

  6. 夺命雷公狗---node.js---6net模块玩telnet通信(下)

    我们来升级玩玩,废话不多说,代码如下所示: /** * Created by leigood on 2016/8/12. */ var net = require('net'); var ChatSr ...

  7. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  8. UIViewController启动过程

    流程:判断是否view属性为nil,如果为空,调用loadView方法,如果重写了loadView方法,那么从代码创建普通视图,如果没有重写并且有storyBoard或者xib文件,那么从storyB ...

  9. 关于 ActiveMQ

    今天玩了下 ActiveMQ,希望实现服务器的消息可以通知到各个客户终端. 安装: 1.安装 ActiveMQ 之前必须安装 Java 的 jdk , 可以从此下载:   http://www.ora ...

  10. 为 Macbook 增加锁屏热键技巧

    第一步,找到“系统偏好设置”下的“安全性与隐私”,在“通用”页里勾上“进入睡眠或开始屏幕保护程序后立即要求输入密码”. 第二步,要用快捷键启动屏幕保护程序,相对复杂一点.在“应用程序”里找到“Auto ...