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

            統計

            常用鏈接

            留言簿

            隨筆分類

            文章分類

            文章檔案

            搜索

            最新評論

            亚洲国产成人久久综合一| 日本精品久久久久久久久免费| 亚洲国产精品无码久久青草| 精品伊人久久久| 欧美黑人激情性久久| 蜜桃麻豆www久久| 久久久久99精品成人片三人毛片 | 伊人久久大香线蕉av不变影院| 漂亮人妻被黑人久久精品| 99久久中文字幕| 一级a性色生活片久久无| 久久精品国产久精国产思思| 国产日韩久久免费影院| 国产精品久久久久a影院| 久久99精品国产一区二区三区 | 少妇久久久久久被弄到高潮| 中文字幕人妻色偷偷久久| 99久久99久久精品国产片| 日韩精品久久无码中文字幕| 久久久久国产亚洲AV麻豆| 精品久久久久久久无码| 热久久最新网站获取| 国产午夜精品久久久久九九电影 | 99久久无色码中文字幕| 欧美国产成人久久精品| 国产成人精品久久| 久久超碰97人人做人人爱| 超级碰碰碰碰97久久久久| 国产亚洲精午夜久久久久久 | 久久久久久久久66精品片| 久久久精品视频免费观看 | 久久久久久噜噜精品免费直播 | 丁香久久婷婷国产午夜视频| 精品久久久久久亚洲精品| 午夜精品久久久久久影视riav| 激情综合色综合久久综合| 精品久久一区二区三区| 久久99国产乱子伦精品免费| 亚洲AV无码久久精品狠狠爱浪潮| 欧美精品丝袜久久久中文字幕| 韩国三级中文字幕hd久久精品 |