字符集之間轉換(UTF-8,UNICODE,Gb2312)

iteye_20309發表於2006-11-07
字符集之間轉換(UTF-8,UNICODE,Gb2312)
特蒐集了UTF-8,UNICODE,Gb2312他們3個之間的相互轉換.
UTF-8: 1~3位元組可變
UNICODE: 2位元組一個字元
GB2312: 2位元組一個字元
例子: “你”字的UTF-8編碼: E4 BD A0        11100100 10111101 10100000
“你”的Unicode編碼: 4F 60          01001111 01100000
按照UTF-8的編碼規則,分解如下:xxxx0100 xx111101 xx100000
把除了x之外的數字拼接在一起,就變成“你”的Unicode編碼了。
注意UTF-8的最前面3個1,表示整個UTF-8串是由3個位元組構成的。
經過UTF-8編碼之後,再也不會出現敏感字元了,因為最高位始終為1。
類定義

class CChineseCode

{

   public:

       static void UTF_8ToUnicode(wchar_t* pOut,char *pText);  // 把UTF-8轉換成Unicode

       static void UnicodeToUTF_8(char* pOut,wchar_t* pText);  //Unicode 轉換成UTF-8

       static void UnicodeToGB2312(char* pOut,wchar_t uData);  // 把Unicode 轉換成 GB2312 

       static void Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer);// GB2312 轉換成 Unicode

       static void GB2312ToUTF_8(string& pOut,char *pText, int pLen);//GB2312 轉為 UTF-8

       static void UTF_8ToGB2312(string &pOut, char *pText, int pLen);//UTF-8 轉為 GB2312

};



類實現



void CChineseCode::UTF_8ToUnicode(wchar_t* 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 CChineseCode::UnicodeToUTF_8(char* pOut,wchar_t* 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 CChineseCode::UnicodeToGB2312(char* pOut,wchar_t uData)

{

    WideCharToMultiByte(CP_ACP,NULL,&uData,1,pOut,sizeof(wchar_t),NULL,NULL);

    return;

}     



void CChineseCode::Gb2312ToUnicode(wchar_t* pOut,char *gbBuffer)

{

    ::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,2,pOut,1);

    return ;

}



void CChineseCode::GB2312ToUTF_8(string& pOut,char *pText, int pLen)

{

    char buf[4];

    int nLength = pLen* 3;

    char* rst = new char[nLength];

    

    memset(buf,0,4);

    memset(rst,0,nLength);

    

    int i = 0;

    int j = 0;      

    while(i < pLen)

    {

            //如果是英文直接複製就可以

            if( *(pText + i) >= 0)

            {

                    rst[j++] = pText[i++];

            }

            else

            {

                    wchar_t pbuffer;

                    Gb2312ToUnicode(&pbuffer,pText+i);

                    

                    UnicodeToUTF_8(buf,&pbuffer);

                    

                    unsigned short int tmp = 0;

                    tmp = rst[j] = buf[0];

                    tmp = rst[j+1] = buf[1];

                    tmp = rst[j+2] = buf[2];    

                    

                    j += 3;

                    i += 2;

            }

    }

    rst[j] = '';



    //返回結果

    pOut = rst;             

    delete []rst;   

    

    return;

}



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

{

    char * newBuf = new char[pLen];

    char Ctemp[4];

    memset(Ctemp,0,4);



    int i =0;

    int j = 0;

    

    while(i < pLen)

    {

        if(pText[i] > 0)

        {

                newBuf[j++] = pText[i++];                       

        }

        else                 

        {

                WCHAR Wtemp;

                UTF_8ToUnicode(&Wtemp,pText + i);

        

                UnicodeToGB2312(Ctemp,Wtemp);

            

                newBuf[j] = Ctemp[0];

                newBuf[j + 1] = Ctemp[1];



                i += 3;    

                j += 2;   

        }

    }

    newBuf[j] = '';

    

    pOut = newBuf;

    delete []newBuf;

    

    return; 

}  

相關文章