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

            統計

            常用鏈接

            留言簿

            隨筆分類

            文章分類

            文章檔案

            搜索

            最新評論

            久久亚洲高清观看| 97久久久久人妻精品专区| 国产农村妇女毛片精品久久 | 狠狠色婷婷久久一区二区| 国产激情久久久久久熟女老人| 中文精品久久久久人妻不卡| 国产91色综合久久免费| 精品熟女少妇aⅴ免费久久| 精品综合久久久久久97| 久久综合综合久久狠狠狠97色88| 亚洲精品国精品久久99热| 国产美女久久久| 国产精品中文久久久久久久| 99久久精品国产综合一区| 亚洲国产精品无码久久| 久久久久九国产精品| 国产精品久久国产精品99盘| 色妞色综合久久夜夜| 国产精品九九久久免费视频 | 亚洲精品高清一二区久久| 精品久久久久中文字幕日本| 97精品伊人久久大香线蕉| 久久精品?ⅴ无码中文字幕| 国产高潮国产高潮久久久| 亚洲AV成人无码久久精品老人| 欧美午夜A∨大片久久| 亚洲国产精品热久久| 久久99热只有频精品8| 中文字幕久久精品无码| 久久久久久伊人高潮影院| 亚洲国产成人久久综合碰| 午夜精品久久影院蜜桃| 久久久久国产一级毛片高清板| 国产亚州精品女人久久久久久 | 亚洲综合伊人久久综合| 久久久午夜精品福利内容| 久久久久久久综合日本| 亚洲精品无码久久毛片| 亚洲精品午夜国产va久久| 狠狠色丁香久久婷婷综合图片| 日韩十八禁一区二区久久 |