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

            統計

            常用鏈接

            留言簿

            隨筆分類

            文章分類

            文章檔案

            搜索

            最新評論

            久久亚洲高清综合| 精品免费久久久久久久| 99久久婷婷国产综合精品草原| 久久强奷乱码老熟女网站| 亚洲精品99久久久久中文字幕| 国产精品久久久久久搜索 | 狠狠色丁香婷婷久久综合 | 久久国产视屏| 久久精品人人做人人爽电影| 精品久久久久久成人AV| 久久精品国产亚洲一区二区三区| 久久久久久久久久久| 一本色道久久88综合日韩精品 | 久久国产精品波多野结衣AV| 国产麻豆精品久久一二三| 久久久精品日本一区二区三区| 日韩人妻无码精品久久久不卡| 伊人久久无码中文字幕| 久久久www免费人成精品| 久久se这里只有精品| 久久精品国产亚洲AV无码偷窥| 久久精品中文字幕无码绿巨人| 欧美成a人片免费看久久| 国产精品久久久天天影视| 99久久精品国产一区二区| 色综合合久久天天给综看| 久久人人青草97香蕉| 久久久久久曰本AV免费免费| 精品久久久无码中文字幕| 国产精品久久久久9999高清| 久久精品国产亚洲AV无码娇色| 国色天香久久久久久久小说| 久久笫一福利免费导航| 亚洲国产精品无码久久久久久曰| 老司机午夜网站国内精品久久久久久久久 | 成人免费网站久久久| 久久久久久亚洲Av无码精品专口 | 99久久久精品免费观看国产| 久久久久99精品成人片试看| 国产精品美女久久久m| 国产精品久久久久久久久|