1. 程式人生 > >unicode與多位元組工程,char與WCHAR_T轉化

unicode與多位元組工程,char與WCHAR_T轉化

#include<string>//標準C++;

string   tp; 
wchar_t   *s; 
char   *d; 
tp=s; 
d=tp.c_str(); 
也可使用WideCharToString(wchar_t   *   Source);函式轉換

WideCharToMultiByte

MultiByteToWideChar

LPSTR = char * 
LPCSTR = const char * 
LPWSTR = wchar_t * 
LPCWSTR = const wchar_t * 
LPOLESTR = OLECHAR * = BSTR = LPWSTR(Win32) 
LPCOLESTR = const OLECHAR * = LPCWSTR(Win32) 
LPTSTR = _TCHAR * 
LPCTSTR = const _TCHAR *

關鍵字: char 和 wchar_t相互轉化

C程式碼 複製程式碼

  1. #include <windows.h>
  2. #include <stdio.h>
  3. //function: charTowchar
  4. //purpose:char to WCHAR 、wchar_t、LPWSTR etc
  5. void charTowchar(const char *chr, wchar_t *wchar, int size)  
  6. {     
  7.     MultiByteToWideChar( CP_ACP, 0, chr,  
  8.         strlen(chr)+1, wchar, size/sizeof(wchar[0]) );  
  9. }  
  10. //function: wcharTochar
  11. //purpose:WCHAR 、wchar_t、LPWSTR to char
  12. void wcharTochar(const wchar_t *wchar, char *chr, int length)  
  13. {  
  14.     WideCharToMultiByte( CP_ACP, 0, wchar, -1,  
  15.         chr, length, NULL, NULL );  
  16. }  
  17. int main (void)  
  18. {  
  19. char     chr[128];  
  20. wchar_t  *wchar = L"陳鴻欽";  
  21. //wchar_t to char
  22.     wcharTochar(wchar, chr, sizeof(chr));  
  23.     printf("char is %s/n", chr);  
  24. //char to wchar_t
  25.     wchar = (wchar_t *)malloc(sizeof(wchar_t) * 64);  
  26.     charTowchar(chr, wchar, sizeof(wchar_t) * 64);  
  27.     wprintf_s(L"%s/n", wchar);//
  28.     getchar();  
  29. return 0;  

函式原型: 
int WideCharToMultiByte
  UINT CodePage, 
  DWORD dwFlags, 
  LPCWSTR lpWideCharStr, 
  int cchWideChar, 
  LPSTR lpMultiByteStr, 
  int cbMultiByte, 
  LPCSTR lpDefaultChar,    
  LPBOOL lpUsedDefaultChar 
);

引數說明: 
1、CodePage 
指定要轉換成的字符集內碼表,它可以是任何已經安裝的或系統自帶的字符集,可以選擇以下的內碼表: 
CP_ACP         //當前系統ANSI內碼表 
CP_MACCP       //當前系統Macintosh內碼表 
CP_OEMCP       //當前系統OEM內碼表,一種原始裝置製造商硬體掃描碼 
CP_SYMBOL      //Symbol內碼表,用於Windows 2000及以後版本 
CP_THREAD_ACP  //當前執行緒ANSI內碼表,用於Windows 2000及以後版本 
CP_UTF7  //UTF-7,設定此值時lpDefaultChar和lpUsedDefaultChar都必須為NULL 
CP_UTF8  //UTF-8,設定此值時lpDefaultChar和lpUsedDefaultChar都必須為NULL

2、dwFlags 
指定如何處理沒有轉換的字元,一般情況下設為0 
WC_NO_BEST_FIT_CHARS  //把不能直接轉換成相應多位元組字元的Unicode字元轉換成lpDefaultChar指定的預設字元。 
WC_COMPOSITECHECK     //把合成字元轉換成預製的字元。它可以與後三個選項中的任何一個組合使用,如果沒有與他們中的任何一個組合,則與選項WC_SEPCHARS相同。 
WC_ERR_INVALID_CHARS  //此選項會致使函式遇到無效字元時失敗返回,並且GetLastError會返回錯誤碼ERROR_NO_UNICODE_TRANSLATION。否則函式會自動丟棄非法字元。此選項只能用於UTF8 
WC_DISCARDNS      //轉換時丟棄不佔空間的字元,與WC_COMPOSITECHECK一起使用 
WC_SEPCHARS       //轉換時產生單獨的字元,此是預設轉換選項,與WC_COMPOSITECHECK一起使用 
WC_DEFAULTCHAR    //轉換時使用預設字元代替例外的字元,(最常見的如"?"),與WC_COMPOSITECHECK一起使用

3、lpWideCharStr  //要轉換的寬字串 
4、cchWideChar    //要轉換寬字串的長度,-1表示轉換到字串結尾 
5、lpMultiByteStr //接收轉換後輸出的字串的緩衝區 
6、cbMultiByte    //輸出緩衝區大小,如果為0,lpMultiByteStr將被忽略,函式將返回所需緩衝區大小而不使用lpMultiByteStr。 
7、lpDefaultChar  //指向字元的指標,在指定編碼裡找不到相應字元時使用此字元作為預設字元代替,如果為NULL則使用系統預設字元 
8、lpUsedDefaultChar  //開關變數的指標,用以表明是否使用過預設字元,可設為NULL

返回值:為0表示呼叫失敗

下面就來看看如何將wstring和wchar_t*轉換為string和char* 
wstring轉換為string:
 
wstring wstr=_T("翔翔糖糖"); 
int size=WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,NULL,0,NULL,NULL); 
char *ch=new char[size+1]; 
if(!WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,ch,size,NULL,NULL)) 

    return false; 

string str=ch;

wchar_t*轉換為char*: 
wchar_t *wch=_T("翔翔糖糖"); 
int size=WideCharToMultiByte(CP_ACP,0,wch,-1,NULL,0,NULL,NULL); 
char *ch=new char[size+1]; 
if(!WideCharToMultiByte(CP_ACP,0,wch,-1,ch,size,NULL,NULL)) 

    return false; 
}

wchar_t* wstr = L"A wide character string.";

char* ascii = new char[wcslen(wstr) + 1];

wcstombs( ascii, wstr, wcslen(wstr) );

另一種方法:                                                                                                                           char* localeInfo;                                                                                                                 wchar_t *pwchello = L"/x3042/x3043"; // 2 Hiragana characters                                       wchar_t *pwc;

wchar_t*轉換為char*:                                                                                                        size_t size;                                                                                                                            int requiredSize;                                                                                                                   requiredSize = wcstombs( NULL, pwchello, 0);                                                                    pmbhello = (unsigned char *)malloc( requiredSize + 1);                                               size = wcstombs( pmbhello, pwchello, requiredSize + 1);

char* 轉換為 wchar_t*                                                                                                       size_t size;                                                                                                                            int requiredSize;                                                                                                                   requiredSize = mbstowcs(NULL, pmbhello, 0);                                                                     pwc = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t ));                                       size = mbstowcs( pwc, pmbhello, requiredSize + 1);

請問怎麼樣把string轉換到LPCTSTR

std::string   str; 
LPCTSTR   lpstr   =   (LPCTSTR)str.c_str();

MFC中Messagebox如何輸出數字