• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-3  評論-13  文章-10  trackbacks-0
            目前的互聯網,是屬于世界的。越來越多的郵件,編碼都是用UTF8。但對于我們習慣還是用ASCII/GB2312來顯示/編輯。所以UTF8的編碼內容需要轉換,特地寫了一個類,來封裝解析的部分。

            /*?UTF8Charset.h?*/
            class?CUTF8Charset??
            {
            public:
            ????CUTF8Charset();
            ????
            virtual?~CUTF8Charset();

            public:
            ????
            static?void?UTF_8ToGB2312(CString?&strOut,?char?*pText,?int?pLen);
            ????
            static?void?GB2312ToUTF_8(CString?&strOut,?char?*pText,?int?pLen);

            ????
            static?void?UTF_8ToGB2312(char?*pOut,?char?*pText,?int?pLen);
            ????
            static?void?GB2312ToUTF_8(char?*pOut,?char?*pText,?int?pLen);

            ????
            //?Unicode?轉換成UTF-8
            ????static?void?UnicodeToUTF_8(char*?pOut,WCHAR*?pText);
            ????
            //?GB2312?轉換成 Unicode
            ????static?void?Gb2312ToUnicode(WCHAR*?pOut,char?*gbBuffer);
            ????
            //?把Unicode?轉換成?GB2312
            ????static?void?UnicodeToGB2312(char*?pOut,?WCHAR*?pText);
            ????
            //?把UTF-8轉換成Unicode
            ????static?void?UTF_8ToUnicode(WCHAR*?pOut,char*?pText);
            }
            ;

            /*?UTF8Charset.cpp?*/
            #include?
            "UTF8Charset.h"

            CUTF8Charset::CUTF8Charset()
            {

            }


            CUTF8Charset::
            ~CUTF8Charset()
            {

            }


            void?CUTF8Charset::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);
            }


            void?CUTF8Charset::UnicodeToGB2312(char*?pOut,?WCHAR*?pText)
            {
            ????::WideCharToMultiByte(CP_ACP,NULL,pText,
            1,pOut,sizeof(WCHAR),NULL,NULL);
            }


            void?CUTF8Charset::Gb2312ToUnicode(WCHAR*?pOut,?char?*gbBuffer)
            {
            ????::MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,gbBuffer,
            2,pOut,1);
            }


            void?CUTF8Charset::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));
            }


            void?CUTF8Charset::GB2312ToUTF_8(char?*pOut,?char?*pText,?int?pLen)
            {
            ????
            char?buf[4];
            ????memset(buf,
            0,4);

            ????
            int?i?=?0;
            ????
            int?j?=?0;????
            ????
            while(i?<?pLen)
            ????
            {
            ????????
            //如果是英文直接復制就可以
            ????????if(?*(pText?+?i)?>=?0)
            ????????
            {
            ????????????pOut[j
            ++]?=?pText[i++];
            ????????}

            ????????
            else
            ????????
            {
            ????????????WCHAR?pbuffer;
            ????????????Gb2312ToUnicode(
            &pbuffer,?pText+i);????????????
            ????????????UnicodeToUTF_8(buf,
            &pbuffer);

            ????????????unsigned?
            short?int?tmp?=?0;
            ????????????tmp?
            =?pOut[j]?=?buf[0];
            ????????????tmp?
            =?pOut[j+1]?=?buf[1];
            ????????????tmp?
            =?pOut[j+2]?=?buf[2];????????????

            ????????????j?
            +=?3;
            ????????????i?
            +=?2;
            ????????}

            ????}

            ????pOut[j]?
            =?'\0';
            }


            void?CUTF8Charset::UTF_8ToGB2312(char?*pOut,?char?*pText,?int?pLen)
            {
            ????
            int?i?=0;
            ????
            int?j?=?0;
            ????
            char?Ctemp[3]?=?"";

            ????
            while(i?<?pLen)
            ????
            {
            ????????
            if(pText[i]?>?0)
            ????????
            {
            ????????????pOut[j
            ++]?=?pText[i++];????????????
            ????????}

            ????????
            else?????????????????
            ????????
            {
            ????????????WCHAR?Wtemp;
            ????????????UTF_8ToUnicode(
            &Wtemp,pText+i);
            ????????????UnicodeToGB2312(Ctemp,
            &Wtemp);

            ????????????pOut[j]?
            =?Ctemp[0];
            ????????????pOut[j?
            +?1]?=?Ctemp[1];

            ????????????i?
            +=?3;????
            ????????????j?
            +=?2;???
            ????????}

            ????}

            ????pOut[j]?
            =?'\0';
            }


            void?CUTF8Charset::GB2312ToUTF_8(CString?&strOut,?char?*pText,?int?pLen)
            {????
            ????
            int?nBufferLen?=?pLen?/?2?*?3;????//按照全是漢字的情況預留空間
            ????char?*rst?=?new?char[nBufferLen];
            ????memset(rst,
            0,nBufferLen);

            ????GB2312ToUTF_8(rst,?pText,?pLen);

            ????strOut?
            =?rst;
            ????delete?[]?rst;
            ????rst?
            =?NULL;
            }


            void?CUTF8Charset::UTF_8ToGB2312(CString?&strOut,?char?*pText,?int?pLen)
            {
            ????
            char?*rst?=?new?char[pLen];
            ????memset(rst,
            0,pLen);

            ????UTF_8ToGB2312(rst,?pText,?pLen);

            ????strOut?
            =?rst;
            ????delete?[]?rst;
            ????rst?
            =?NULL;
            }
            posted on 2006-05-12 15:07 雙魚座的程序員 閱讀(679) 評論(0)  編輯 收藏 引用
            久久精品www人人爽人人| 污污内射久久一区二区欧美日韩 | 一97日本道伊人久久综合影院| 怡红院日本一道日本久久| 久久国产精品免费一区| 久久久久99这里有精品10 | 久久伊人精品青青草原高清| 亚洲人成无码www久久久| 国产精品久久久久天天影视| 人人狠狠综合久久亚洲高清| 国产精品毛片久久久久久久| 久久精品免费全国观看国产| 久久99精品国产麻豆宅宅| 国产香蕉久久精品综合网| 久久99免费视频| 国产偷久久久精品专区 | 国产亚洲精久久久久久无码| 久久香蕉国产线看观看猫咪?v| 人妻无码αv中文字幕久久琪琪布 人妻无码久久一区二区三区免费 人妻无码中文久久久久专区 | 久久精品国产精品青草| 久久久精品人妻一区二区三区蜜桃| 老司机国内精品久久久久| 久久国产欧美日韩精品| 久久久久亚洲AV综合波多野结衣| 国产欧美一区二区久久| 久久综合久久自在自线精品自| 色天使久久综合网天天| 久久久久亚洲精品中文字幕| 久久本道综合久久伊人| Xx性欧美肥妇精品久久久久久| 欧美一区二区精品久久| 国产成人精品久久免费动漫| 国产V综合V亚洲欧美久久| 久久婷婷五月综合97色一本一本| 久久受www免费人成_看片中文 | 国产精品久久国产精品99盘| 久久久久人妻一区精品色| 少妇内射兰兰久久| 久久精品国产清高在天天线| 97久久久精品综合88久久| 日本精品久久久久中文字幕|