• <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 厚積薄發 閱讀(471) 評論(0)  編輯 收藏 引用 所屬分類: Windows編程

            導航

            <2025年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿

            隨筆分類

            文章分類

            文章檔案

            搜索

            最新評論

            国内精品伊人久久久久av一坑| 久久久国产视频| 色综合久久久久| 日韩亚洲欧美久久久www综合网| 亚洲国产精久久久久久久| 国产精品熟女福利久久AV| 色婷婷综合久久久久中文字幕| 久久久久亚洲av综合波多野结衣| 久久午夜无码鲁丝片秋霞| 久久精品国产亚洲AV香蕉| 久久精品国产WWW456C0M| 国产精品成人久久久| 国产精品美女久久久久网| 久久久精品人妻无码专区不卡| 狠狠精品久久久无码中文字幕| 久久91精品国产91久久麻豆| 伊人久久大香线蕉AV一区二区| 麻豆AV一区二区三区久久| 无码8090精品久久一区| 国产精品久久久福利| 久久人人爽人人人人片av| 成人亚洲欧美久久久久| 久久av无码专区亚洲av桃花岛| 久久精品国产一区二区三区不卡| 久久久久亚洲av无码专区导航| 久久国产精品一区| 久久精品国产99国产电影网| 中文国产成人精品久久不卡 | 91视频国产91久久久| 漂亮人妻被中出中文字幕久久 | 久久99精品国产麻豆宅宅| 亚洲日韩中文无码久久| 日韩久久无码免费毛片软件| 久久免费线看线看| 久久96国产精品久久久| 无码人妻久久久一区二区三区| 国产精品久久久久久久人人看| 婷婷久久综合九色综合绿巨人| 久久久国产精品| 欧美色综合久久久久久| 久久人人爽人人澡人人高潮AV |