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

有三分代码

strCoding.h

strCoding.cpp

test.cpp

 

*******************************************************************************************************************************

 

  1. //这是个类strCoding (strCoding.h文件)   
  2. #pragma once   
  3. #include <iostream>   
  4. #include <string>   
  5. #include <windows.h>   
  6. using namespace std;  
  7.   
  8. class strCoding  
  9. {  
  10. public:  
  11.     strCoding(void);  
  12.     ~strCoding(void);  
  13.      
  14.     void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//utf_8转为gb2312   
  15.     void GB2312ToUTF_8(string& pOut,char *pText, int pLen); //gb2312 转utf_8   
  16.     string UrlGB2312(char * str);                           //urlgb2312编码   
  17.     string UrlUTF8(char * str);                             //urlutf8 编码   
  18.     string UrlUTF8Decode(string str);                  //urlutf8解码   
  19.     string UrlGB2312Decode(string str);                //urlgb2312解码   
  20.     
  21. private:  
  22.     void Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer);  
  23.     void UTF_8ToUnicode(WCHAR* pOut,char *pText);  
  24.     void UnicodeToUTF_8(char* pOut,WCHAR* pText);  
  25.     void UnicodeToGB2312(char* pOut,WCHAR uData);  
  26.     char  CharToInt(char ch);  
  27.     char StrToBin(char *str);  
  28.   
  29. };  
//这是个类strCoding (strCoding.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);



};

 

 

*******************************************************************************************************************************

  1. //这是个类strCoding (strCoding.cpp文件)   
  2. #include "StdAfx.h"   
  3.   
  4. #include ".\strcoding.h"   
  5.   
  6.   
  7.   
  8. strCoding::strCoding(void)  
  9. {  
  10. }  
  11.   
  12. strCoding::~strCoding(void)  
  13. {  
  14. }  
  15. void strCoding::Gb2312ToUnicode(WCHAR* pOut,char *gbBuffer)  
  16. {  
  17.     ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);  
  18.     return;  
  19. }  
  20. void strCoding::UTF_8ToUnicode(WCHAR* pOut,char *pText)  
  21. {  
  22.     char* uchar = (char *)pOut;  
  23.        
  24.     uchar[1] = ((pText[0] & 0x0F) << 4) + ((pText[1] >> 2) & 0x0F);  
  25.     uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);  
  26.   
  27.     return;  
  28. }  
  29.   
  30. void strCoding::UnicodeToUTF_8(char* pOut,WCHAR* pText)  
  31. {  
  32.     // 注意 WCHAR高低字的顺序,低字节在前,高字节在后   
  33.     char* pchar = (char *)pText;  
  34.   
  35.     pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));  
  36.     pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 6);  
  37.     pOut[2] = (0x80 | (pchar[0] & 0x3F));  
  38.   
  39.     return;  
  40. }  
  41. void strCoding::UnicodeToGB2312(char* pOut,WCHAR uData)  
  42. {  
  43.     WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(WCHAR),NULL,NULL);  
  44.     return;  
  45. }  
  46.   
  47. //做为解Url使用   
  48. char strCoding:: CharToInt(char ch){  
  49.         if(ch>='0' && ch<='9')return (char)(ch-'0');  
  50.         if(ch>='a' && ch<='f')return (char)(ch-'a'+10);  
  51.         if(ch>='A' && ch<='F')return (char)(ch-'A'+10);  
  52.         return -1;  
  53. }  
  54. char strCoding::StrToBin(char *str){  
  55.         char tempWord[2];  
  56.         char chn;  
  57.   
  58.         tempWord[0] = CharToInt(str[0]);                         //make the B to 11 -- 00001011   
  59.         tempWord[1] = CharToInt(str[1]);                         //make the 0 to 0  -- 00000000   
  60.   
  61.         chn = (tempWord[0] << 4) | tempWord[1];                //to change the BO to 10110000   
  62.   
  63.         return chn;  
  64. }  
  65.   
  66.   
  67. //UTF_8 转gb2312   
  68. void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)  
  69. {  
  70.      char buf[4];  
  71.      char* rst = new char[pLen + (pLen >> 2) + 2];  
  72.     memset(buf,0,4);  
  73.     memset(rst,0,pLen + (pLen >> 2) + 2);  
  74.   
  75.     int i =0;  
  76.     int j = 0;  
  77.         
  78.     while(i < pLen)  
  79.     {  
  80.         if(*(pText + i) >= 0)  
  81.         {  
  82.               
  83.             rst[j++] = pText[i++];  
  84.         }  
  85.         else                   
  86.         {  
  87.             WCHAR Wtemp;  
  88.   
  89.               
  90.             UTF_8ToUnicode(&Wtemp,pText + i);  
  91.                 
  92.             UnicodeToGB2312(buf,Wtemp);  
  93.               
  94.             unsigned short int tmp = 0;  
  95.             tmp = rst[j] = buf[0];  
  96.             tmp = rst[j+1] = buf[1];  
  97.             tmp = rst[j+2] = buf[2];  
  98.   
  99.             //newBuf[j] = Ctemp[0];   
  100.             //newBuf[j + 1] = Ctemp[1];   
  101.   
  102.             i += 3;      
  103.             j += 2;     
  104.         }  
  105.           
  106.   }  
  107.     rst[j]='\0';  
  108.    pOut = rst;   
  109.     delete []rst;  
  110. }  
  111.   
  112. //GB2312 转为 UTF-8   
  113. void strCoding::GB2312ToUTF_8(string& pOut,char *pText, int pLen)  
  114. {  
  115.     char buf[4];  
  116.     memset(buf,0,4);  
  117.   
  118.     pOut.clear();  
  119.   
  120.     int i = 0;  
  121.     while(i < pLen)  
  122.     {  
  123.         //如果是英文直接复制就可以   
  124.         if( pText[i] >= 0)  
  125.         {  
  126.             char asciistr[2]={0};  
  127.             asciistr[0] = (pText[i++]);  
  128.             pOut.append(asciistr);  
  129.         }  
  130.         else  
  131.         {  
  132.             WCHAR pbuffer;  
  133.             Gb2312ToUnicode(&pbuffer,pText+i);  
  134.   
  135.             UnicodeToUTF_8(buf,&pbuffer);  
  136.   
  137.             pOut.append(buf);  
  138.   
  139.             i += 2;  
  140.         }  
  141.     }  
  142.   
  143.     return;  
  144. }  
  145. //把str编码为网页中的 GB2312 url encode ,英文不变,汉字双字节  如%3D%AE%88   
  146. string strCoding::UrlGB2312(char * str)  
  147. {  
  148.     string dd;  
  149.     size_t len = strlen(str);  
  150.     for (size_t i=0;i<len;i++)  
  151.     {  
  152.         if(isalnum((BYTE)str[i]))  
  153.         {  
  154.             char tempbuff[2];  
  155.             sprintf(tempbuff,"%c",str[i]);  
  156.             dd.append(tempbuff);  
  157.         }  
  158.         else if (isspace((BYTE)str[i]))  
  159.         {  
  160.             dd.append("+");  
  161.         }  
  162.         else  
  163.         {  
  164.             char tempbuff[4];  
  165.             sprintf(tempbuff,"%%%X%X",((BYTE*)str)[i] >>4,((BYTE*)str)[i] %16);  
  166.             dd.append(tempbuff);  
  167.         }  
  168.   
  169.     }  
  170.     return dd;  
  171. }  
  172.   
  173. //把str编码为网页中的 UTF-8 url encode ,英文不变,汉字三字节  如%3D%AE%88   
  174.   
  175. string strCoding::UrlUTF8(char * str)  
  176. {  
  177.     string tt;  
  178.     string dd;  
  179.     GB2312ToUTF_8(tt,str,(int)strlen(str));  
  180.   
  181.     size_t len=tt.length();  
  182.     for (size_t i=0;i<len;i++)  
  183.     {  
  184.         if(isalnum((BYTE)tt.at(i)))  
  185.         {  
  186.             char tempbuff[2]={0};  
  187.             sprintf(tempbuff,"%c",(BYTE)tt.at(i));  
  188.             dd.append(tempbuff);  
  189.         }  
  190.         else if (isspace((BYTE)tt.at(i)))  
  191.         {  
  192.             dd.append("+");  
  193.         }  
  194.         else  
  195.         {  
  196.             char tempbuff[4];  
  197.             sprintf(tempbuff,"%%%X%X",((BYTE)tt.at(i)) >>4,((BYTE)tt.at(i)) %16);  
  198.             dd.append(tempbuff);  
  199.         }  
  200.   
  201.     }  
  202.     return dd;  
  203. }  
  204. //把url GB2312解码   
  205. string strCoding::UrlGB2312Decode(string str)  
  206. {  
  207.    string output="";  
  208.         char tmp[2];  
  209.         int i=0,idx=0,ndx,len=str.length();  
  210.           
  211.         while(i<len){  
  212.                 if(str[i]=='%'){  
  213.                         tmp[0]=str[i+1];  
  214.                         tmp[1]=str[i+2];  
  215.                         output += StrToBin(tmp);  
  216.                         i=i+3;  
  217.                 }  
  218.                 else if(str[i]=='+'){  
  219.                         output+=' ';  
  220.                         i++;  
  221.                 }  
  222.                 else{  
  223.                         output+=str[i];  
  224.                         i++;  
  225.                 }  
  226.         }  
  227.           
  228.         return output;  
  229. }  
  230. //把url utf8解码   
  231. string strCoding::UrlUTF8Decode(string str)  
  232. {  
  233.      string output="";  
  234.   
  235.     string temp =UrlGB2312Decode(str);//   
  236.   
  237.     UTF_8ToGB2312(output,(char *)temp.data(),strlen(temp.data()));  
  238.   
  239.     return output;  
  240.   
  241. }  
//这是个类strCoding (strCoding.cpp文件)

#include "StdAfx.h"



#include ".\strcoding.h"







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) << 4) + ((pText[1] >> 2) & 0x0F);

    uchar[0] = ((pText[1] & 0x03) << 6) + (pText[2] & 0x3F);



    return;

}



void strCoding::UnicodeToUTF_8(char* pOut,WCHAR* pText)

{

    // 注意 WCHAR高低字的顺序,低字节在前,高字节在后

    char* pchar = (char *)pText;



    pOut[0] = (0xE0 | ((pchar[1] & 0xF0) >> 4));

    pOut[1] = (0x80 | ((pchar[1] & 0x0F) << 2)) + ((pchar[0] & 0xC0) >> 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<='9')return (char)(ch-'0');

        if(ch>='a' && ch<='f')return (char)(ch-'a'+10);

        if(ch>='A' && ch<='F')return (char)(ch-'A'+10);

        return -1;

}

char strCoding::StrToBin(char *str){

        char tempWord[2];

        char chn;



        tempWord[0] = CharToInt(str[0]);                         //make the B to 11 -- 00001011

        tempWord[1] = CharToInt(str[1]);                         //make the 0 to 0  -- 00000000



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



        return chn;

}





//UTF_8 转gb2312

void strCoding::UTF_8ToGB2312(string &pOut, char *pText, int pLen)

{

     char buf[4];

     char* rst = new char[pLen + (pLen >> 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<len;i++)

    {

        if(isalnum((BYTE)str[i]))

        {

            char tempbuff[2];

            sprintf(tempbuff,"%c",str[i]);

            dd.append(tempbuff);

        }

        else if (isspace((BYTE)str[i]))

        {

            dd.append("+");

        }

        else

        {

            char tempbuff[4];

            sprintf(tempbuff,"%%%X%X",((BYTE*)str)[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<len;i++)

    {

        if(isalnum((BYTE)tt.at(i)))

        {

            char tempbuff[2]={0};

            sprintf(tempbuff,"%c",(BYTE)tt.at(i));

            dd.append(tempbuff);

        }

        else if (isspace((BYTE)tt.at(i)))

        {

            dd.append("+");

        }

        else

        {

            char tempbuff[4];

            sprintf(tempbuff,"%%%X%X",((BYTE)tt.at(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<len){

                if(str[i]=='%'){

                        tmp[0]=str[i+1];

                        tmp[1]=str[i+2];

                        output += StrToBin(tmp);

                        i=i+3;

                }

                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;



}

 

*******************************************************************************************************************************

 

  1. //test   
  2. #include "stdafx.h"   
  3. #include "strCoding.h"   
  4.   
  5. using namespace std;  
  6.   
  7.   
  8. int main()  
  9. {   
  10.       
  11.      strCoding cfm;  
  12.      string keyword="大家好,欢迎你";  
  13.      string Temp="";  
  14.      string Output="";  
  15.   
  16.      //把关键字做url的utf8编码   
  17.      Temp= cfm.UrlUTF8((char *)keyword.data());  
  18.      cout<<Temp<<endl;  
  19.       
  20.      //把url的utf8编码的结果解码   
  21.      Temp =cfm.UrlUTF8Decode(Temp);  
  22.      cout<<Temp<<endl;  
  23.   
  24.      //把关键字做url的gb2312编码   
  25.      Temp =cfm.UrlGB2312((char *)keyword.data());  
  26.      cout<<Temp<<endl;  
  27.        
  28.      //把url的gb2312编码的结果解码   
  29.      Temp =cfm.UrlGB2312Decode(Temp);  
  30.      cout<<Temp<<endl;  
  31.   
  32.   
  33.      //把关键字GB2312转UTF_8   
  34.       
  35.      cfm.GB2312ToUTF_8(Output,(char *)keyword.data(),strlen(keyword.data()));  
  36.      cout<<Output<<endl;  
  37.   
  38.      //把GB2312转UTF_8转为中文   
  39.      cfm.UTF_8ToGB2312(Temp,(char *)Output.data(),strlen(Output.data()));  
  40.      cout<<Temp<<endl;  
  41.   
  42.   
  43.     //system("pasue");   
  44.      getchar();  
  45.   
  46.     return 0;  
  47.     //   
  48. }  

你可能感兴趣的:(c/c++)