C/C++ 字符编码的转换(ut8、gb2312)

本文介绍了一个用于字符串编码转换的C++类strCoding,该类支持UTF-8与GB2312之间的相互转换,以及URL编码与解码。文章还提供了一个简单的测试示例,演示了如何使用这个类进行各种编码转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif//这是个类strCoding (strCoding.h文件)
None.gif
#pragma once
None.gif#include 
<iostream>
None.gif#include 
<string>
None.gif#include 
<windows.h>
None.gif
using namespace std;
None.gif
None.gif
class strCoding
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public:
InBlock.gif    strCoding(
void);
InBlock.gif    
~strCoding(void);
InBlock.gif   
InBlock.gif    
void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//utf_8转为gb2312
InBlock.gif
    void GB2312ToUTF_8(string& pOut,char *pText, int pLen); //gb2312 转utf_8
InBlock.gif
    string UrlGB2312(char * str);                           //urlgb2312编码
InBlock.gif
    string UrlUTF8(char * str);                             //urlutf8 编码
InBlock.gif
    string UrlUTF8Decode(string str);                  //urlutf8解码
InBlock.gif
    string UrlGB2312Decode(string str);                //urlgb2312解码
InBlock.gif
  
InBlock.gif
private:
InBlock.gif    
void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer);
InBlock.gif    
void UTF_8ToUnicode(WCHAR* pOut,char *pText);
InBlock.gif    
void UnicodeToUTF_8(char* pOut,WCHAR* pText);
InBlock.gif    
void UnicodeToGB2312(char* pOut,WCHAR uData);
InBlock.gif    
char  CharToInt(char ch);
InBlock.gif    
char StrToBin(char *str);
InBlock.gif
ExpandedBlockEnd.gif}
;
None.gif
None.gif//这是个类strCoding (strCoding.cpp文件)
None.gif
#include "StdAfx.h"
None.gif
None.gif#include 
".\strcoding.h"
None.gif
None.gif
None.gif
None.gifstrCoding::strCoding(
void)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedBlockEnd.gif}

None.gif
None.gifstrCoding::
~strCoding(void)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedBlockEnd.gif}

None.gif
void strCoding::Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,
2,pOut,1);
InBlock.gif    
return;
ExpandedBlockEnd.gif}

None.gif
void strCoding::UTF_8ToUnicode(WCHAR* pOut,char *pText)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
char* uchar = (char *)pOut;
InBlock.gif     
InBlock.gif    uchar[
1= ((pText[0& 0x0F<< 4+ ((pText[1>> 2& 0x0F);
InBlock.gif    uchar[
0= ((pText[1& 0x03<< 6+ (pText[2& 0x3F);
InBlock.gif
InBlock.gif    
return;
ExpandedBlockEnd.gif}

None.gif
None.gif
void strCoding::UnicodeToUTF_8(char* pOut,WCHAR* pText)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// 注意 WCHAR高低字的顺序,低字节在前,高字节在后
InBlock.gif
    char* pchar = (char *)pText;
InBlock.gif
InBlock.gif    pOut[
0= (0xE0 | ((pchar[1& 0xF0>> 4));
InBlock.gif    pOut[
1= (0x80 | ((pchar[1& 0x0F<< 2)) + ((pchar[0& 0xC0>> 6);
InBlock.gif    pOut[
2= (0x80 | (pchar[0& 0x3F));
InBlock.gif
InBlock.gif    
return;
ExpandedBlockEnd.gif}

None.gif
void strCoding::UnicodeToGB2312(char* pOut,WCHAR uData)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    WideCharToMultiByte(CP_ACP,NULL,
&uData,1,pOut,sizeof(WCHAR),NULL,NULL);
InBlock.gif    
return;
ExpandedBlockEnd.gif}

None.gif
None.gif
//做为解Url使用
ExpandedBlockStart.gifContractedBlock.gif
char strCoding:: CharToInt(char ch)dot.gif{
InBlock.gif        
if(ch>='0' && ch<='9')return (char)(ch-'0');
InBlock.gif        
if(ch>='a' && ch<='f')return (char)(ch-'a'+10);
InBlock.gif        
if(ch>='A' && ch<='F')return (char)(ch-'A'+10);
InBlock.gif        
return -1;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
char strCoding::StrToBin(char *str)dot.gif{
InBlock.gif        
char tempWord[2];
InBlock.gif        
char chn;
InBlock.gif
InBlock.gif        tempWord[
0= CharToInt(str[0]);                         //make the B to 11 -- 00001011
InBlock.gif
        tempWord[1= CharToInt(str[1]);                         //make the 0 to 0  -- 00000000
InBlock.gif

InBlock.gif        chn 
= (tempWord[0<< 4| tempWord[1];                //to change the BO to 10110000
InBlock.gif

InBlock.gif        
return chn;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
//UTF_8 转gb2312
None.gif
void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif     
char buf[4];
InBlock.gif     
char* rst = new char[pLen + (pLen >> 2+ 2];
InBlock.gif    memset(buf,
0,4);
InBlock.gif    memset(rst,
0,pLen + (pLen >> 2+ 2);
InBlock.gif
InBlock.gif    
int i =0;
InBlock.gif    
int j = 0;
InBlock.gif      
InBlock.gif    
while(i < pLen)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(*(pText + i) >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
InBlock.gif            rst[j
++= pText[i++];
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else                 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WCHAR Wtemp;
InBlock.gif
InBlock.gif            
InBlock.gif            UTF_8ToUnicode(
&Wtemp,pText + i);
InBlock.gif              
InBlock.gif            UnicodeToGB2312(buf,Wtemp);
InBlock.gif            
InBlock.gif            unsigned 
short int tmp = 0;
InBlock.gif            tmp 
= rst[j] = buf[0];
InBlock.gif            tmp 
= rst[j+1= buf[1];
InBlock.gif            tmp 
= rst[j+2= buf[2];
InBlock.gif
InBlock.gif            
//newBuf[j] = Ctemp[0];
InBlock.gif            
//newBuf[j + 1] = Ctemp[1];
InBlock.gif

InBlock.gif            i 
+= 3;    
InBlock.gif            j 
+= 2;   
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif  }

InBlock.gif    rst[j]
='\0';
InBlock.gif   pOut 
= rst; 
InBlock.gif    delete []rst;
ExpandedBlockEnd.gif}

None.gif
None.gif
//GB2312 转为 UTF-8
None.gif
void strCoding::GB2312ToUTF_8(string& pOut,char *pText, int pLen)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
char buf[4];
InBlock.gif    memset(buf,
0,4);
InBlock.gif
InBlock.gif    pOut.clear();
InBlock.gif
InBlock.gif    
int i = 0;
InBlock.gif    
while(i < pLen)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//如果是英文直接复制就可以
InBlock.gif
        if( pText[i] >= 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
char asciistr[2]=dot.gif{0};
InBlock.gif            asciistr[
0= (pText[i++]);
InBlock.gif            pOut.append(asciistr);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            WCHAR pbuffer;
InBlock.gif            Gb2312ToUnicode(
&pbuffer,pText+i);
InBlock.gif
InBlock.gif            UnicodeToUTF_8(buf,
&pbuffer);
InBlock.gif
InBlock.gif            pOut.append(buf);
InBlock.gif
InBlock.gif            i 
+= 2;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
return;
ExpandedBlockEnd.gif}

None.gif
//把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节  如%3D%AE%88
None.gif
string strCoding::UrlGB2312(char * str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
string dd;
InBlock.gif    size_t len 
= strlen(str);
InBlock.gif    
for (size_t i=0;i<len;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(isalnum((BYTE)str[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
char tempbuff[2];
InBlock.gif            sprintf(tempbuff,
"%c",str[i]);
InBlock.gif            dd.append(tempbuff);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if (isspace((BYTE)str[i]))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dd.append(
"+");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
char tempbuff[4];
InBlock.gif            sprintf(tempbuff,
"%%%X%X",((BYTE*)str)[i] >>4,((BYTE*)str)[i] %16);
InBlock.gif            dd.append(tempbuff);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return dd;
ExpandedBlockEnd.gif}

None.gif
None.gif
//把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节  如%3D%AE%88
None.gif

None.gif
string strCoding::UrlUTF8(char * str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
string tt;
InBlock.gif    
string dd;
InBlock.gif    GB2312ToUTF_8(tt,str,(
int)strlen(str));
InBlock.gif
InBlock.gif    size_t len
=tt.length();
InBlock.gif    
for (size_t i=0;i<len;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(isalnum((BYTE)tt.at(i)))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
char tempbuff[2]=dot.gif{0};
InBlock.gif            sprintf(tempbuff,
"%c",(BYTE)tt.at(i));
InBlock.gif            dd.append(tempbuff);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if (isspace((BYTE)tt.at(i)))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            dd.append(
"+");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
char tempbuff[4];
InBlock.gif            sprintf(tempbuff,
"%%%X%X",((BYTE)tt.at(i)) >>4,((BYTE)tt.at(i)) %16);
InBlock.gif            dd.append(tempbuff);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return dd;
ExpandedBlockEnd.gif}

None.gif
//把url GB2312解码
None.gif
string strCoding::UrlGB2312Decode(string str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif   
string output="";
InBlock.gif        
char tmp[2];
InBlock.gif        
int i=0,idx=0,ndx,len=str.length();
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
while(i<len)dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
if(str[i]=='%')dot.gif{
InBlock.gif                        tmp[
0]=str[i+1];
InBlock.gif                        tmp[
1]=str[i+2];
InBlock.gif                        output 
+= StrToBin(tmp);
InBlock.gif                        i
=i+3;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
else if(str[i]=='+')dot.gif{
InBlock.gif                        output
+=' ';
InBlock.gif                        i
++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockStart.gifContractedSubBlock.gif                
elsedot.gif{
InBlock.gif                        output
+=str[i];
InBlock.gif                        i
++;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
return output;
ExpandedBlockEnd.gif}

None.gif
//把url utf8解码
None.gif
string strCoding::UrlUTF8Decode(string str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif     
string output="";
InBlock.gif
InBlock.gif    
string temp =UrlGB2312Decode(str);//
InBlock.gif

InBlock.gif    UTF_8ToGB2312(output,(
char *)temp.data(),strlen(temp.data()));
InBlock.gif
InBlock.gif    
return output;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif//test
None.gif
#include "stdafx.h"
None.gif
#include "strCoding.h"
None.gif
None.gif
using namespace std;
None.gif
None.gif
None.gif
int main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
InBlock.gif     strCoding cfm;
InBlock.gif     
string keyword="大家好,欢迎你";
InBlock.gif     
string Temp="";
InBlock.gif     
string Output="";
InBlock.gif
InBlock.gif     
//把关键字做url的utf8编码
InBlock.gif
     Temp= cfm.UrlUTF8((char *)keyword.data());
InBlock.gif     cout
<<Temp<<endl;
InBlock.gif    
InBlock.gif     
//把url的utf8编码的结果解码
InBlock.gif
     Temp =cfm.UrlUTF8Decode(Temp);
InBlock.gif     cout
<<Temp<<endl;
InBlock.gif
InBlock.gif     
//把关键字做url的gb2312编码
InBlock.gif
     Temp =cfm.UrlGB2312((char *)keyword.data());
InBlock.gif     cout
<<Temp<<endl;
InBlock.gif     
InBlock.gif     
//把url的gb2312编码的结果解码
InBlock.gif
     Temp =cfm.UrlGB2312Decode(Temp);
InBlock.gif     cout
<<Temp<<endl;
InBlock.gif
InBlock.gif
InBlock.gif     
//把关键字GB2312转UTF_8
InBlock.gif
    
InBlock.gif     cfm.GB2312ToUTF_8(Output,(
char *)keyword.data(),strlen(keyword.data()));
InBlock.gif     cout
<<Output<<endl;
InBlock.gif
InBlock.gif     
//把GB2312转UTF_8转为中文
InBlock.gif
     cfm.UTF_8ToGB2312(Temp,(char *)Output.data(),strlen(Output.data()));
InBlock.gif     cout
<<Temp<<endl;
InBlock.gif
InBlock.gif
InBlock.gif    
//system("pasue");
InBlock.gif
     getchar();
InBlock.gif
InBlock.gif    
return 0;
InBlock.gif    
//
ExpandedBlockEnd.gif
}


在VC7win32下调试通过

转载于:https://www.cnblogs.com/cfam/archive/2007/07/29/835301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值