• <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  評(píng)論-13  文章-10  trackbacks-0
            目前的互聯(lián)網(wǎng),是屬于世界的。越來越多的郵件,編碼都是用UTF8。但對(duì)于我們習(xí)慣還是用ASCII/GB2312來顯示/編輯。所以UTF8的編碼內(nèi)容需要轉(zhuǎn)換,特地寫了一個(gè)類,來封裝解析的部分。

            /*?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?轉(zhuǎn)換成UTF-8
            ????static?void?UnicodeToUTF_8(char*?pOut,WCHAR*?pText);
            ????
            //?GB2312?轉(zhuǎn)換成 Unicode
            ????static?void?Gb2312ToUnicode(WCHAR*?pOut,char?*gbBuffer);
            ????
            //?把Unicode?轉(zhuǎn)換成?GB2312
            ????static?void?UnicodeToGB2312(char*?pOut,?WCHAR*?pText);
            ????
            //?把UTF-8轉(zhuǎn)換成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高低字的順序,低字節(jié)在前,高字節(jié)在后
            ????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)
            ????
            {
            ????????
            //如果是英文直接復(fù)制就可以
            ????????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;????//按照全是漢字的情況預(yù)留空間
            ????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 雙魚座的程序員 閱讀(676) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久精品9988| 久久只这里是精品66| 久久亚洲高清观看| 久久久久无码精品| 国产A三级久久精品| 国产一级持黄大片99久久| 久久国产视屏| 久久Av无码精品人妻系列| 国产成人精品久久| 无码久久精品国产亚洲Av影片| 久久精品成人免费看| 伊人久久大香线蕉综合Av| 国产精品99久久久久久董美香| 久久精品人妻中文系列| 99久久成人18免费网站| 狼狼综合久久久久综合网| 日批日出水久久亚洲精品tv| 精品久久777| 久久男人Av资源网站无码软件| 久久久久99精品成人片| 久久91精品久久91综合| 亚洲精品无码久久久久久| 日本精品久久久久影院日本| 国产精品福利一区二区久久| 狠狠色丁香婷婷久久综合五月 | 中文字幕乱码人妻无码久久| 久久线看观看精品香蕉国产| 久久99精品国产自在现线小黄鸭| 超级97碰碰碰碰久久久久最新| 91性高湖久久久久| 久久96国产精品久久久| 77777亚洲午夜久久多喷| 久久99精品国产麻豆| 久久中文骚妇内射| 性高湖久久久久久久久| 少妇久久久久久被弄高潮| 色8久久人人97超碰香蕉987| 伊人久久大香线蕉av一区| 久久综合香蕉国产蜜臀AV| 久久国产精品无码HDAV| 99久久这里只有精品|