MultiByteToWideChar和WideCharToMultiByte用法詳解

//========================================================================
//TITLE:
//    MultiByteToWideChar和WideCharToMultiByte用法詳解
//AUTHOR:
//    norains
//DATE:
//    第一版:Monday  25-December -2006
//    增補版:Wednesday 27-December -2006
//    修訂版:Wednesday 14-March-2007 (修正之前的錯誤例子)
//Environment:
//  EVC4.0 + Standard SDK
//========================================================================
 
1.使用方法詳解

  在本文開始之處,先簡要地說一下何爲短字符和寬字符.
  所謂的短字符,就是用8bit來表示的字符,典型的應用是ASCII碼.而寬字符,顧名思義,就是用16bit表示的字符,典型的有UNICODE.關於windows下的ASCII和UNICODE的更多信息,可以參考這兩本經典著作:《windows 程序設計》,《windows 核心編程》.這兩本書關於這兩種字符都有比較詳細的解說.
 
  寬字符轉換爲多個短字符是一個難點,不過我們只要掌握到其中的要領,便可如魚得水.
  好吧,那就讓我們開始吧.
 
  這個是我們需要轉化的多字節字符串:  
  char sText[20] = {"多字節字符串!OK!"};
 
  我們需要知道轉化後的寬字符需要多少個數組空間.雖然在這個里程裏面,我們可以直接定義一個20*2寬字符的數組,並且事實上將運行得非常輕鬆愉快.但假如多字節字符串更多,達到上千個乃至上萬個,我們將會發現其中浪費的內存將會越來越多.所以以多字節字符的個數的兩倍作爲寬字符數組下標的聲明絕對不是一個好主意.
  所幸,我們能夠確知所需要的數組空間.
  我們只需要將MultiByteToWideChar()的第四個形參設爲-1,即可返回所需的短字符數組空間的個數:
  DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, sText, -1, NULL, 0);
 
  接下來,我們只需要分配響應的數組空間:
  wchar_t *pwText;
  pwText = new wchar_t[dwNum];
  if(!pwText)
  {
   delete []pwText;
  }
 
  接着,我們就可以着手進行轉換了.在這裏以轉換成ASCII碼做爲例子:
  MultiByteToWideChar (CP_ACP, 0, psText, -1, sText, dwSize);
 
  最後,使用完畢當然要記得釋放佔用的內存:
  delete []psText;
 
 
  同理,寬字符轉爲多字節字符的代碼如下:  
  wchar_t wText[20] = {L"寬字符轉換實例!OK!"};
  DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
  char *psText;
  psText = new char[dwNum];
  if(!psText)
  {
   delete []psText;
  }
  WideCharToMultiByte (CP_OEMCP,NULL,lpcwszStr,-1,psText,dwNum,NULL,FALSE);
  delete []psText;
 
   如果之前我們已經分配好空間,並且由於字符串較短,可以不理會浪費的空間,僅僅只是想簡單地將短字符和寬字符相互轉換,那有沒有什麼簡便的方法呢?
   WIN32 API裏沒有符合這種要求的函數,但我們可以自己進行封裝:
     
  //-------------------------------------------------------------------------------------
  //Description:
  // This function maps a character string to a wide-character (Unicode) string
  //
  //Parameters:
  // lpcszStr: [in] Pointer to the character string to be converted
  // lpwszStr: [out] Pointer to a buffer that receives the translated string.
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  //
  //Example:
  // MByteToWChar(szA,szW,sizeof(szW)/sizeof(szW[0]));
  //---------------------------------------------------------------------------------------
  BOOL MByteToWChar(LPCSTR lpcszStr, LPWSTR lpwszStr, DWORD dwSize)
  {
    // Get the required size of the buffer that receives the Unicode
    // string.
    DWORD dwMinSize;
    dwMinSize = MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, NULL, 0);
 
    if(dwSize < dwMinSize)
    {
     return FALSE;
    }
 
    
    // Convert headers from ASCII to Unicode.
    MultiByteToWideChar (CP_ACP, 0, lpcszStr, -1, lpwszStr, dwMinSize);  
    return TRUE;
  }
 
  //-------------------------------------------------------------------------------------
  //Description:
  // This function maps a wide-character string to a new character string
  //
  //Parameters:
  // lpcwszStr: [in] Pointer to the character string to be converted
  // lpszStr: [out] Pointer to a buffer that receives the translated string.
  // dwSize: [in] Size of the buffer
  //
  //Return Values:
  // TRUE: Succeed
  // FALSE: Failed
  //
  //Example:
  // MByteToWChar(szW,szA,sizeof(szA)/sizeof(szA[0]));
  //---------------------------------------------------------------------------------------
  BOOL WCharToMByte(LPCWSTR lpcwszStr, LPSTR lpszStr, DWORD dwSize)
  {
   DWORD dwMinSize;
   dwMinSize = WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,NULL,0,NULL,FALSE);
   if(dwSize < dwMinSize)
   {
    return FALSE;
   }
   WideCharToMultiByte(CP_OEMCP,NULL,lpcwszStr,-1,lpszStr,dwSize,NULL,FALSE);
   return TRUE;
  }
 
 
  使用方法也很簡單,示例如下:
  wchar_t wText[10] = {L"函數示例"};
  char sText[20]= {0};
  WCharToMByte(wText,sText,sizeof(sText)/sizeof(sText[0]));
  MByteToWChar(sText,wText,sizeof(wText)/sizeof(wText[0]));
 
  這兩個函數的缺點在於無法動態分配內存,在轉換很長的字符串時可能會浪費較多內存空間;優點是,在不考慮浪費空間的情況下轉換較短字符串非常方便.

 
2.MultiByteToWideChar()函數亂碼的問題

  有的朋友可能已經發現,在標準的WinCE4.2或WinCE5.0 SDK模擬器下,這個函數都無法正常工作,其轉換之後的字符全是亂碼.及時更改MultiByteToWideChar()參數也依然如此.
  不過這個不是代碼問題,其結症在於所定製的操作系統.如果我們定製的操作系統默認語言不是中文,也會出現這種情況.由於標準的SDK默認語言爲英文,所以肯定會出現這個問題.而這個問題的解決,不能在簡單地更改控制面板的"區域選項"的"默認語言",而是要在系統定製的時候,選擇默認語言爲"中文".
  系統定製時選擇默認語言的位置於:

  Platform -> Setting... -> locale -> default language ,選擇"中文",然後編譯即可.

==================================================================================================================================

**************************************************************************************************************************************************************************************

==================================================================================================================================

WideCharToMultiByte和MultiByteToWideChar函數的用法

爲了支持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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章