当前位置: 编程技术>c/c++/嵌入式
C++类URL编码和解码使用技巧
来源: 互联网 发布时间:2014-10-11
本文导语: 在项目开发过程中,经常会使用到c++ 的url编码和解码,本文将以此问题详细介绍使用技巧,需要的朋友可以参考下 代码如下: #pragma once #include #include #include using namespace std; class strCoding { public: strCoding(void); ~strCoding(void); void U...
在项目开发过程中,经常会使用到c++ 的url编码和解码,本文将以此问题详细介绍使用技巧,需要的朋友可以参考下
#pragma once
#include
#include
#include
using namespace std;
class strCoding
{
public:
strCoding(void);
~strCoding(void);
void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//utf_8转为gb2312
void GB2312ToUTF_8(string& pOut,char *pText, int pLen); //gb2312 转utf_8
string UrlGB2312(char * str); //urlgb2312编码
string UrlUTF8(char * str); //urlutf8 编码
string UrlUTF8Decode(string str); //urlutf8解码
string UrlGB2312Decode(string str); //urlgb2312解码
private:
void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer);
void UTF_8ToUnicode(WCHAR* pOut,char *pText);
void UnicodeToUTF_8(char* pOut,WCHAR* pText);
void UnicodeToGB2312(char* pOut,WCHAR uData);
char CharToInt(char ch);
char StrToBin(char *str);
};
#include "StdAfx.h"
#include "./urlcodeing.h"
//这是个类strCoding (strCoding.cpp文件)
strCoding::strCoding(void)
{
}
strCoding::~strCoding(void)
{
}
void strCoding::Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return;
}
void strCoding::UTF_8ToUnicode(WCHAR* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) > 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) > 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) > 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void strCoding::UnicodeToGB2312(char* pOut,WCHAR uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
return;
}
//做为解Url使用
char strCoding:: CharToInt(char ch){
if(ch>='0' && ch='a' && ch='A' && ch 2) + 2];
memset(buf,0,4);
memset(rst,0,pLen + (pLen >> 2) + 2);
int i =0;
int j = 0;
while(i < pLen)
{
if(*(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(buf,Wtemp);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
//newBuf[j] = Ctemp[0];
//newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
rst[j]='/0';
pOut = rst;
delete []rst;
}
//GB2312 转为 UTF-8
void strCoding::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
memset(buf,0,4);
pOut.clear();
int i = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( pText[i] >= 0)
{
char asciistr[2]={0};
asciistr[0] = (pText[i++]);
pOut.append(asciistr);
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
pOut.append(buf);
i += 2;
}
}
return;
}
//把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节 如%3D%AE%88
string strCoding::UrlGB2312(char * str)
{
string dd;
size_t len = strlen(str);
for (size_t i=0;i>4,((BYTE*)str)[i] %16);
dd.append(tempbuff);
}
}
return dd;
}
//把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节 如%3D%AE%88
string strCoding::UrlUTF8(char * str)
{
string tt;
string dd;
GB2312ToUTF_8(tt,str,(int)strlen(str));
size_t len=tt.length();
for (size_t i=0;i>4,((BYTE)tt.at(i)) %16);
dd.append(tempbuff);
}
}
return dd;
}
//把url GB2312解码
string strCoding::UrlGB2312Decode(string str)
{
string output="";
char tmp[2];
int i=0,idx=0,ndx,len=str.length();
while(i
代码如下:
#pragma once
#include
#include
#include
using namespace std;
class strCoding
{
public:
strCoding(void);
~strCoding(void);
void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//utf_8转为gb2312
void GB2312ToUTF_8(string& pOut,char *pText, int pLen); //gb2312 转utf_8
string UrlGB2312(char * str); //urlgb2312编码
string UrlUTF8(char * str); //urlutf8 编码
string UrlUTF8Decode(string str); //urlutf8解码
string UrlGB2312Decode(string str); //urlgb2312解码
private:
void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer);
void UTF_8ToUnicode(WCHAR* pOut,char *pText);
void UnicodeToUTF_8(char* pOut,WCHAR* pText);
void UnicodeToGB2312(char* pOut,WCHAR uData);
char CharToInt(char ch);
char StrToBin(char *str);
};
代码如下:
#include "StdAfx.h"
#include "./urlcodeing.h"
//这是个类strCoding (strCoding.cpp文件)
strCoding::strCoding(void)
{
}
strCoding::~strCoding(void)
{
}
void strCoding::Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)
{
::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);
return;
}
void strCoding::UTF_8ToUnicode(WCHAR* pOut,char *pText)
{
char* uchar = (char *)pOut;
uchar[1] = ((pText[0] & 0x0F) > 2) & 0x0F);
uchar[0] = ((pText[1] & 0x03) > 4));
pOut[1] = (0x80 | ((pchar[1] & 0x0F) > 6);
pOut[2] = (0x80 | (pchar[0] & 0x3F));
return;
}
void strCoding::UnicodeToGB2312(char* pOut,WCHAR uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
return;
}
//做为解Url使用
代码如下:
char strCoding:: CharToInt(char ch){
if(ch>='0' && ch='a' && ch='A' && ch 2) + 2];
memset(buf,0,4);
memset(rst,0,pLen + (pLen >> 2) + 2);
int i =0;
int j = 0;
while(i < pLen)
{
if(*(pText + i) >= 0)
{
rst[j++] = pText[i++];
}
else
{
WCHAR Wtemp;
UTF_8ToUnicode(&Wtemp,pText + i);
UnicodeToGB2312(buf,Wtemp);
unsigned short int tmp = 0;
tmp = rst[j] = buf[0];
tmp = rst[j+1] = buf[1];
tmp = rst[j+2] = buf[2];
//newBuf[j] = Ctemp[0];
//newBuf[j + 1] = Ctemp[1];
i += 3;
j += 2;
}
}
rst[j]='/0';
pOut = rst;
delete []rst;
}
//GB2312 转为 UTF-8
代码如下:
void strCoding::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[4];
memset(buf,0,4);
pOut.clear();
int i = 0;
while(i < pLen)
{
//如果是英文直接复制就可以
if( pText[i] >= 0)
{
char asciistr[2]={0};
asciistr[0] = (pText[i++]);
pOut.append(asciistr);
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i);
UnicodeToUTF_8(buf,&pbuffer);
pOut.append(buf);
i += 2;
}
}
return;
}
//把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节 如%3D%AE%88
代码如下:
string strCoding::UrlGB2312(char * str)
{
string dd;
size_t len = strlen(str);
for (size_t i=0;i>4,((BYTE*)str)[i] %16);
dd.append(tempbuff);
}
}
return dd;
}
//把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节 如%3D%AE%88
代码如下:
string strCoding::UrlUTF8(char * str)
{
string tt;
string dd;
GB2312ToUTF_8(tt,str,(int)strlen(str));
size_t len=tt.length();
for (size_t i=0;i>4,((BYTE)tt.at(i)) %16);
dd.append(tempbuff);
}
}
return dd;
}
//把url GB2312解码
代码如下:
string strCoding::UrlGB2312Decode(string str)
{
string output="";
char tmp[2];
int i=0,idx=0,ndx,len=str.length();
while(i