2011年7月19日 星期二

wchar_t 和 char 的轉換

從Microsoft Document Explorer 上找到的資料。

The wchar_t type is defined by MIDL as an unsigned short (16-bit) data object.

The keyword char identifies a data item that has 8 bits. To the MIDL compiler, a char is unsigned by default and is synonymous with unsigned char.

C++標準中,wchar_t是寬字元類型,每個wchar_t類型占2個位元組,16位元寬。漢字的表示就要用到wchar_t 。char,我們都知道,占一個位元組,8位元寬。


附上最近用到的轉換程式(BCB 2009 - C++):

//---------------------------------------------------------------------------
// Anistring Char To wchar_t
//---------------------------------------------------------------------------
wchar_t* TPreviewForm::AnsiToUnicode( const char* szStr )
{
wchar_t wszUNICODE[65535];
char szBIG5[65535];
char szUTF8[65535];
int len=0;
len = MultiByteToWideChar(CP_UTF8, 0, szStr, -1, NULL,0); // UTF8-->UNICODE
if (len == 0)
return NULL;
len = (len+1)*2;
MultiByteToWideChar(CP_UTF8, 0, szStr, -1, wszUNICODE, len);
return wszUNICODE;
}

//---------------------------------------------------------------------------
// UTF8 Char To wchar_t
//---------------------------------------------------------------------------
wchar_t* TPreviewForm::UTF8ToWChar(const char *pszGbs)
{
 wchar_t wszUNICODE[65535];
 char szBIG5[65535];
 char szUTF8[65535];
 int len=0;
 len = MultiByteToWideChar(CP_UTF8, 0, pszGbs, -1, NULL,0); // UTF8-->UNICODE
 if (len == 0)
  return NULL;
 len = (len+1)*2;
 MultiByteToWideChar(CP_UTF8, 0, pszGbs, -1, wszUNICODE, len);
 return wszUNICODE;
}

//---------------------------------------------------------------------------
// wchar_t*  To char*
//---------------------------------------------------------------------------
char* TPreviewForm::UnicodeToAnsi(const wchar_t* szStr )
{
 int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
 if (nLen == 0)
 {
   return NULL;
 }
 char* pResult = new char[nLen];
 WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
 return pResult;
}

沒有留言:

張貼留言