• <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函數的用法

            轉自http://www.cnblogs.com/gakusei/articles/1585211.html

            為了支持Unicode編碼,需要多字節與寬字節之間的相互轉換。這兩個系統函數在使用時需要指定代碼頁,在實際應用過程中遇到亂碼問題,然后重新閱讀《Windows核心編程》,總結出正確的用法。
            WideCharToMultiByte的代碼頁用來標記與新轉換的字符串相關的代碼頁。
            MultiByteToWideChar的代碼頁用來標記與一個多字節字符串相關的代碼頁。
            常用的代碼頁由CP_ACP和CP_UTF8兩個。
            使用CP_ACP代碼頁就實現了ANSI與Unicode之間的轉換。
            使用CP_UTF8代碼頁就實現了UTF-8與Unicode之間的轉換。
            下面是代碼實現:
            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 厚積薄發 閱讀(481) 評論(0)  編輯 收藏 引用 所屬分類: Windows編程

            導航

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            統計

            常用鏈接

            留言簿

            隨筆分類

            文章分類

            文章檔案

            搜索

            最新評論

            欧美午夜精品久久久久免费视| 亚洲AV日韩AV天堂久久| 久久综合丁香激情久久| 久久国产精品一区| 色婷婷综合久久久久中文| 国产91久久综合| 亚洲国产欧美国产综合久久| 久久99国产精一区二区三区| 久久婷婷五月综合成人D啪| 久久99精品久久久久久动态图| 久久久久国产一区二区| 久久夜色精品国产欧美乱| 青青久久精品国产免费看| 99re久久精品国产首页2020| 2020国产成人久久精品| 久久免费精品视频| 日日噜噜夜夜狠狠久久丁香五月| 久久免费大片| 99久久精品国产一区二区| 欧美大香线蕉线伊人久久| 久久91精品综合国产首页| 999久久久无码国产精品| 无码人妻久久久一区二区三区 | 曰曰摸天天摸人人看久久久| 久久久久久精品久久久久| 久久伊人五月天论坛| 色噜噜狠狠先锋影音久久| 久久亚洲高清观看| 国产精品18久久久久久vr | 久久丫忘忧草产品| 天天影视色香欲综合久久| 久久夜色精品国产| 色偷偷88欧美精品久久久| 久久久久综合中文字幕| 久久99精品九九九久久婷婷| 国产精品亚洲综合专区片高清久久久 | 久久人人爽人人爽人人片AV不| 久久精品成人欧美大片| 伊人久久一区二区三区无码| 93精91精品国产综合久久香蕉 | 91精品国产综合久久香蕉 |