• <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>

            yehao's Blog

            WideCharToMultiByte和MultiByteToWideChar函數(shù)的用法

            轉(zhuǎn)自http://www.cnblogs.com/gakusei/articles/1585211.html

            為了支持Unicode編碼,需要多字節(jié)與寬字節(jié)之間的相互轉(zhuǎn)換。這兩個(gè)系統(tǒng)函數(shù)在使用時(shí)需要指定代碼頁(yè),在實(shí)際應(yīng)用過(guò)程中遇到亂碼問(wèn)題,然后重新閱讀《Windows核心編程》,總結(jié)出正確的用法。
            WideCharToMultiByte的代碼頁(yè)用來(lái)標(biāo)記與新轉(zhuǎn)換的字符串相關(guān)的代碼頁(yè)。
            MultiByteToWideChar的代碼頁(yè)用來(lái)標(biāo)記與一個(gè)多字節(jié)字符串相關(guān)的代碼頁(yè)。
            常用的代碼頁(yè)由CP_ACP和CP_UTF8兩個(gè)。
            使用CP_ACP代碼頁(yè)就實(shí)現(xiàn)了ANSI與Unicode之間的轉(zhuǎn)換。
            使用CP_UTF8代碼頁(yè)就實(shí)現(xiàn)了UTF-8與Unicode之間的轉(zhuǎn)換。
            下面是代碼實(shí)現(xiàn):
            1.  ANSI to Unicode
            wstring ANSIToUnicode( const string& str )
            {
             int  len = 0;
             len = str.length();
             int  unicodeLen = ::MultiByteToWideChar( CP_ACP,
                        0,
                        str.c_str(),
                        -1,
                        NULL,
                        0 ); 
             wchar_t *  pUnicode; 
             pUnicode = new  wchar_t[unicodeLen+1]; 
             memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
             ::MultiByteToWideChar( CP_ACP,
                     0,
                     str.c_str(),
                     -1,
                     (LPWSTR)pUnicode,
                     unicodeLen ); 
             wstring  rt; 
             rt = ( wchar_t* )pUnicode;
             delete  pUnicode;
             
             return  rt; 
            }
            2.  Unicode to ANSI
            string UnicodeToANSI( const wstring& str )
            {
             char*     pElementText;
             int    iTextLen;
             // wide char to multi char
             iTextLen = WideCharToMultiByte( CP_ACP,
                     0,
                     str.c_str(),
                     -1,
                     NULL,
                     0,
            NULL,
                     NULL );
             pElementText = new char[iTextLen + 1];
             memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
             ::WideCharToMultiByte( CP_ACP,
                     0,
                     str.c_str(),
                     -1,
                     pElementText,
                     iTextLen,
                     NULL,
                     NULL );
             string strText;
             strText = pElementText;
             delete[] pElementText;
             return strText;
            }
            3.  UTF-8 to Unicode
            wstring UTF8ToUnicode( const string& str )
            {
             int  len = 0;
             len = str.length();
             int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,
                        0,
                        str.c_str(),
                        -1,
                        NULL,
                        0 ); 
             wchar_t *  pUnicode; 
             pUnicode = new  wchar_t[unicodeLen+1]; 
             memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); 
             ::MultiByteToWideChar( CP_UTF8,
                     0,
                     str.c_str(),
                     -1,
                     (LPWSTR)pUnicode,
                     unicodeLen ); 
             wstring  rt; 
             rt = ( wchar_t* )pUnicode;
             delete  pUnicode;
             
             return  rt; 
            }
            4.  Unicode to UTF-8   
            string UnicodeToUTF8( const wstring& str )
            {
             char*     pElementText;
             int    iTextLen;
             // wide char to multi char
             iTextLen = WideCharToMultiByte( CP_UTF8,
                     0,
                     str.c_str(),
                     -1,
                     NULL,
                     0,
                     NULL,
                     NULL );
             pElementText = new char[iTextLen + 1];
             memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
             ::WideCharToMultiByte( CP_UTF8,
                     0,
                     str.c_str(),
                     -1,
                     pElementText,
                     iTextLen,
                     NULL,
                     NULL );
             string strText;
             strText = pElementText;
             delete[] pElementText;
             return strText;
            }

            posted on 2014-05-12 16:43 厚積薄發(fā) 閱讀(483) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Windows編程

            導(dǎo)航

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            統(tǒng)計(jì)

            常用鏈接

            留言簿

            隨筆分類

            文章分類

            文章檔案

            搜索

            最新評(píng)論

            亚洲狠狠综合久久| 国产精品久久久久蜜芽| 99精品久久精品一区二区| 日本人妻丰满熟妇久久久久久| 久久这里只有精品18| 亚洲精品高清久久| 久久人人爽人人人人片av| 精品久久久久久久久午夜福利| 国内精品久久久久久久coent| 久久精品桃花综合| 丰满少妇高潮惨叫久久久| 精品欧美一区二区三区久久久| 久久久SS麻豆欧美国产日韩| 久久91精品国产91久久户| 久久久不卡国产精品一区二区| 一本一本久久A久久综合精品 | 91久久国产视频| 国产综合成人久久大片91| 久久九九久精品国产免费直播| 热99re久久国超精品首页| 日韩人妻无码精品久久免费一| 久久国产成人午夜aⅴ影院 | 一本色综合久久| 久久综合综合久久狠狠狠97色88| 久久久久久国产a免费观看黄色大片| 91精品国产91久久久久久| 日日躁夜夜躁狠狠久久AV| 久久伊人五月丁香狠狠色| 日韩久久无码免费毛片软件| 久久亚洲国产欧洲精品一| 99久久精品国产免看国产一区| 人妻精品久久久久中文字幕一冢本| 久久精品中文字幕第23页| 丁香五月综合久久激情| 久久最新精品国产| 国产99久久精品一区二区| 99久久无码一区人妻a黑| 97久久精品无码一区二区| 久久精品国产清高在天天线| 久久精品九九亚洲精品| 国产午夜免费高清久久影院|