C++ url编解码类
2016-12-26 13:33
190 查看
//UrlConvert.h #ifndef __URLCONVERT_H__ #define __URLCONVERT_H__ #pragma once /* 使用方法: CUrlConvert url; //Encode test string str1 = url.UrlEncode_GBK("谷姐"); cout << "UrlEncode_GBK " << str1.c_str() <<endl; string str2 = url.UrlEncode_UTF8("谷姐"); cout << "UrlEncode_UTF8 " << str2.c_str() <<endl; //Decode test string str3 = url.UrlDecode_GBK(str1); cout << "UrlDecode_GBK " << str3.c_str() <<endl; string str4 = url.UrlDecode_UTF8(str2); cout << "UrlDecode_UTF8 " << str4.c_str() <<endl; */ #include <Windows.h> #include <string> class CUrlConvert { public: CUrlConvert(void); ~CUrlConvert(void); // URL编码,编码为GBK std::string UrlEncode_GBK(std::string strOrg); // URL编码,编码为UTF-8 std::string UrlEncode_UTF8(std::string strOrg); // URL解码,解码为GBK std::string UrlDecode_GBK(std::string strOrg); // URL解码,解码为UTF-8 std::string UrlDecode_UTF8(std::string strOrg); private: // URL解码,解码为GBK std::string URLDecode(std::string& strOrg); // URL编码,编码为GBK std::string URLEncode(std::string& strOrg); std::string GBKToUTF8(const std::string &strGBK); std::string UTF8ToGBK(const std::string &strUTF8); }; #endif //__URLCONVERT_H__
//UrlConvert.cpp #include "UrlConvert.h" byte toHex(const byte &x) { return x > 9 ? x -10 + 'A': x + '0'; } byte fromHex(const byte &x) { return isdigit(x) ? x-'0' : x-'A'+10; } CUrlConvert::CUrlConvert(void) { } CUrlConvert::~CUrlConvert(void) { } // URL解码,解码为GBK std::string CUrlConvert::URLDecode(std::string& strOrg) { std::string sOut; for( size_t ix = 0; ix < strOrg.size(); ix++ ) { byte ch = 0; if(strOrg[ix]=='%') { ch = (fromHex(strOrg[ix+1])<<4); ch |= fromHex(strOrg[ix+2]); ix += 2; } else if(strOrg[ix] == '+') { ch = ' '; } else { ch = strOrg[ix]; } sOut += (char)ch; } return sOut; } // URL编码,编码为GBK std::string CUrlConvert::URLEncode(std::string& strOrg) { std::string sOut; for( size_t ix = 0; ix < strOrg.size(); ix++ ) { byte buf[4]; memset( buf, 0, 4 ); if( isalnum( (byte)strOrg[ix] ) ) { buf[0] = strOrg[ix]; } //else if ( isspace( (byte)strOrg[ix] ) ) //貌似把空格编码成%20或者+都可以 //{ // buf[0] = '+'; //} else { buf[0] = '%'; buf[1] = toHex( (byte)strOrg[ix] >> 4 ); buf[2] = toHex( (byte)strOrg[ix] % 16); } sOut += (char *)buf; } return sOut; } std::string CUrlConvert::GBKToUTF8(const std::string &strGBK) { std::string strOutUTF8 = ""; int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), - 1, NULL, 0); wchar_t *str1 = new wchar_t ; MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), - 1, str1, n); n = WideCharToMultiByte(CP_UTF8, 0, str1, - 1, NULL, 0, NULL, NULL); char *str2 = new char ; WideCharToMultiByte(CP_UTF8, 0, str1, - 1, str2, n, NULL, NULL); strOutUTF8 = str2; delete [] str1; delete [] str2; return strOutUTF8; } std::string CUrlConvert::UTF8ToGBK(const std::string &strUTF8) { int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), - 1, NULL, 0); wchar_t *wszGBK = new wchar_t[len + 1]; memset(wszGBK, 0, (len+1)*sizeof(WCHAR)); MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUTF8.c_str(), - 1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, - 1, NULL, 0, NULL, NULL); char *szGBK = new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte(CP_ACP, 0, wszGBK, - 1, szGBK, len, NULL, NULL); //strUTF8 = szGBK; std::string strTemp(szGBK); delete [] szGBK; delete [] wszGBK; return strTemp; } // URL编码,编码为GBK std::string CUrlConvert::UrlEncode_GBK(std::string strOrg) { return URLEncode(strOrg); } // URL编码,编码为UTF-8 std::string CUrlConvert::UrlEncode_UTF8(std::string strOrg) { return URLEncode(GBKToUTF8(strOrg)); } // URL解码,解码为GBK std::string CUrlConvert::UrlDecode_GBK(std::string strOrg) { return URLDecode(strOrg); } // URL解码,解码为UTF-8 std::string CUrlConvert::UrlDecode_UTF8(std::string strOrg) { return UTF8ToGBK(URLDecode(strOrg)); }
// main.cpp // 使用方法 #include "stdafx.h" #include "UrlConvert.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { CUrlConvert url; //Encode test string str1 = url.UrlEncode_GBK("谷姐"); cout << "UrlEncode_GBK " << str1.c_str() <<endl; string str2 = url.UrlEncode_UTF8("谷姐"); cout << "UrlEncode_UTF8 " << str2.c_str() <<endl; //Decode test string str3 = url.UrlDecode_GBK(str1); cout << "UrlDecode_GBK " << str3.c_str() <<endl; string str4 = url.UrlDecode_UTF8(str2); cout << "UrlDecode_UTF8 " << str4.c_str() <<endl; return 0; }
相关文章推荐
- jQuery ajax serialize()方法的使用以及常见问题解决
- php的urlencode()URL编码函数浅析
- asp.net URL编码与解码
- 百度移动版的url编码解码示例
- javascript URL编码和解码使用说明
- URL编码
- Other:网页get与post传输过程中URL的编码问题
- 文件下载 解决文件名中文乱码
- Web常见编码及变换类型识别 (Python编程实现)
- URL编码以及GET和POST提交乱码解决方案
- URL编码函数
- C++中使用IXMLHTTPRequest对象调用Web API时,对URL中的特殊字符(或中文)编码
- HTML URL编码,这个是地址栏常看到的知识
- url 编码
- iOS之POST请求数组样式参数DES加密问题
- URL编码
- HTTP/URL编码以及静态网页和动态网页
- newlisp url 编码
- iOS URL编码&base64编码
- PHP--------解决网址URL编码问题