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

            亚洲人成电影网站久久| 欧美大战日韩91综合一区婷婷久久青草 | 国产三级观看久久| 亚洲а∨天堂久久精品| 97久久综合精品久久久综合| 久久人妻少妇嫩草AV无码蜜桃| 午夜精品久久久久久影视riav| 久久婷婷五月综合色奶水99啪| 青青草国产97免久久费观看| 久久综合狠狠综合久久综合88| 国内精品久久久久久不卡影院 | 久久午夜综合久久| 亚洲午夜久久久久久久久电影网 | 日产精品久久久久久久性色| 99久久成人18免费网站| 久久99免费视频| 97久久国产亚洲精品超碰热| 91久久国产视频| 久久精品国产亚洲av高清漫画 | 国产午夜精品理论片久久影视| 一级a性色生活片久久无| 久久久久人妻精品一区三寸蜜桃 | 伊人热热久久原色播放www| 亚洲国产精品久久久久婷婷软件| 亚洲精品国精品久久99热一| 久久亚洲欧美国产精品| 一本久久免费视频| 四虎亚洲国产成人久久精品| 国产叼嘿久久精品久久| 2021精品国产综合久久| 久久91精品国产91久久麻豆| 国内精品久久久久久99蜜桃| 国产精品久久精品| 久久精品国产福利国产秒| 97久久精品无码一区二区天美| 日韩精品久久无码人妻中文字幕| 亚洲精品乱码久久久久久蜜桃图片| 久久九九兔免费精品6| 久久精品中文騷妇女内射| 久久久久久久久久久久中文字幕 | 久久婷婷色综合一区二区|