下面的代码实现了一个用于C++中转码的类strCoding。里面有UTF8、UNICODE、GB2312编码的互相转换。
.H文件:
#pragma once #include <iostream>
#include <string>
#include <windows.h>
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); };
.cpp
#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,,pOut,);
return;
}
void strCoding::UTF_8ToUnicode(WCHAR* pOut,char *pText)
{
char* uchar = (char *)pOut; uchar[] = ((pText[] & 0x0F) << ) + ((pText[] >> ) & 0x0F);
uchar[] = ((pText[] & 0x03) << ) + (pText[] & 0x3F); return;
} void strCoding::UnicodeToUTF_8(char* pOut,WCHAR* pText)
{
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
char* pchar = (char *)pText; pOut[] = (0xE0 | ((pchar[] & 0xF0) >> ));
pOut[] = (0x80 | ((pchar[] & 0x0F) << )) + ((pchar[] & 0xC0) >> );
pOut[] = (0x80 | (pchar[] & 0x3F)); return;
}
void strCoding::UnicodeToGB2312(char* pOut,WCHAR uData)
{
WideCharToMultiByte(CP_ACP,NULL,&uData,,pOut,sizeof(WCHAR),NULL,NULL);
return;
} //做为解Url使用
char strCoding:: CharToInt(char ch){
if(ch>='' && ch<='')return (char)(ch-'');
if(ch>='a' && ch<='f')return (char)(ch-'a'+);
if(ch>='A' && ch<='F')return (char)(ch-'A'+);
return -;
}
char strCoding::StrToBin(char *str){
char tempWord[];
char chn; tempWord[] = CharToInt(str[]); //make the B to 11 -- 00001011
tempWord[] = CharToInt(str[]); //make the 0 to 0 -- 00000000 chn = (tempWord[] << ) | tempWord[]; //to change the BO to 10110000 return chn;
} //UTF_8 转gb2312
void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
{
char buf[];
char* rst = new char[pLen + (pLen >> ) + ];
memset(buf,,);
memset(rst,,pLen + (pLen >> ) + ); int i =;
int j = ; while(i < pLen)
{
if(*(pText + i) >= )
{ rst[j++] = pText[i++];
}
else
{
WCHAR Wtemp; UTF_8ToUnicode(&Wtemp,pText + i); UnicodeToGB2312(buf,Wtemp); unsigned short int tmp = ;
tmp = rst[j] = buf[];
tmp = rst[j+] = buf[];
tmp = rst[j+] = buf[]; //newBuf[j] = Ctemp[0];
//newBuf[j + 1] = Ctemp[1]; i += ;
j += ;
} }
rst[j]='\0';
pOut = rst;
delete []rst;
} //GB2312 转为 UTF-8
void strCoding::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
{
char buf[];
memset(buf,,); pOut.clear(); int i = ;
while(i < pLen)
{
//如果是英文直接复制就可以
if( pText[i] >= )
{
char asciistr[]={};
asciistr[] = (pText[i++]);
pOut.append(asciistr);
}
else
{
WCHAR pbuffer;
Gb2312ToUnicode(&pbuffer,pText+i); UnicodeToUTF_8(buf,&pbuffer); pOut.append(buf); i += ;
}
} return;
}
//把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节 如%3D%AE%88
string strCoding::UrlGB2312(char * str)
{
string dd;
size_t len = strlen(str);
for (size_t i=;i<len;i++)
{
if(isalnum((BYTE)str[i]))
{
char tempbuff[];
sprintf(tempbuff,"%c",str[i]);
dd.append(tempbuff);
}
else if (isspace((BYTE)str[i]))
{
dd.append("+");
}
else
{
char tempbuff[];
sprintf(tempbuff,"%%%X%X",((BYTE*)str)[i] >>,((BYTE*)str)[i] %);
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=;i<len;i++)
{
if(isalnum((BYTE)tt.at(i)))
{
char tempbuff[]={};
sprintf(tempbuff,"%c",(BYTE)tt.at(i));
dd.append(tempbuff);
}
else if (isspace((BYTE)tt.at(i)))
{
dd.append("+");
}
else
{
char tempbuff[];
sprintf(tempbuff,"%%%X%X",((BYTE)tt.at(i)) >>,((BYTE)tt.at(i)) %);
dd.append(tempbuff);
} }
return dd;
}
//把url GB2312解码
string strCoding::UrlGB2312Decode(string str)
{
string output="";
char tmp[];
int i=,idx=,ndx,len=str.length(); while(i<len){
if(str[i]=='%'){
tmp[]=str[i+];
tmp[]=str[i+];
output += StrToBin(tmp);
i=i+;
}
else if(str[i]=='+'){
output+=' ';
i++;
}
else{
output+=str[i];
i++;
}
} return output;
}
//把url utf8解码
string strCoding::UrlUTF8Decode(string str)
{
string output=""; string temp =UrlGB2312Decode(str);// UTF_8ToGB2312(output,(char *)temp.data(),strlen(temp.data())); return output; }