char 轉wchar_t 及wchar_t轉char
原文來自 http://haofu123.blog.163.com/blog/static/178294920096243161997/利用widechartomultibyte來轉換的函數
通常適合於window平台上使用
#include <windows.h>
int _tmain( int argc, _tchar* argv[])
{
wchar_t pwstr[] =l"我是中國人";
wchar_t pwstr2[20];
char *pcstr = ( char *)malloc( sizeof( char)*(2 * wcslen(pwstr)+1));
memset(pcstr , 0 , 2 * wcslen(pwstr)+1 );
w2c(pcstr,pwstr,2 * wcslen(pwstr)+1) ;
printf("%s\n",pcstr);
c2w(pwstr2,20,pcstr);
wprintf(l"%s",pwstr2);
free(pcstr) ;
return 0;
}
// 將wchar_t* 轉成char*的實現函數如下:
char *w2c( char *pcstr, const wchar_t *pwstr, size_t len)
{
int nlength=wcslen(pwstr);
// 獲取轉換后的長度
int nbytes = WideCharToMultiByte( 0, // specify the code page used to perform the conversion
0, // no special flags to handle unmapped characters
pwstr, // wide character string to convert
nlength, // the number of wide characters in that string
NULL, // no output buffer given, we just want to know how long it needs to be
0,
NULL, // no replacement character given
NULL ); // we don't want to know if a character didn't make it through the translation
// make sure the buffer is big enough for this, making it larger if necessary
if(nbytes>len) nbytes=len;
// 通過以上得到的結果,轉換unicode 字符為ascii 字符
WideCharToMultiByte( 0, // specify the code page used to perform the conversion
0, // no special flags to handle unmapped characters
pwstr, // wide character string to convert
nlength, // the number of wide characters in that string
pcstr, // put the output ascii characters at the end of the buffer
nbytes, // there is at least this much space there
NULL, // no replacement character given
NULL );
return pcstr ;
}
// 將char* 轉成wchar_t*的實現函數如下:
// 這是把asii字符轉換為unicode字符,和上面相同的原理
void c2w(wchar_t *pwstr,size_t len, const char *str)
{
if(str)
{
size_t nu = strlen(str);
size_t n =(size_t)multibytetowidechar(cp_acp,0,( const char *)str,( int)nu, null,0);
if(n>=len)n=len-1;
multibytetowidechar(cp_acp,0,( const char *)str,( int)nu,pwstr,( int)n);
pwstr[n]=0;
}
}
或者用此種方法更好一些:============我自已做的
// 把ascii 字符轉換為unicode字符
wchar_t* Cphone_hq::ctow(wchar_t *pwstr, const char *str)
{
wchar_t* buffer;
if(str)
{
size_t nu = strlen(str);
size_t n =(size_t)MultiByteToWideChar(CP_ACP,0,( const char *)str, int(nu),NULL,0);
buffer=0;
buffer = new wchar_t[n+1];
// if(n>=len) n=len-1;
::MultiByteToWideChar(CP_ACP,0,( const char *)str, int(nu),buffer, int(n));
}
return buffer;
delete buffer;
}
相關知識點:
Unicode的出現是為了適應軟件國際化的需要。Unicode不同於雙字節字符集(DBCS)。
一、相關操作函數
1、DBCS使用下面的函數操作字符串:
CharNext——獲得后一個字符
CharPrev——獲得前一個字符
IsDBCSLeadByte——判斷是否為兩個字節字符的第一個字節
C++運行期庫提供了以"_mbs"開頭的一系列的函數操作DBCS。類似的函數有_mbscat等。
2、ANSI字符集是一個美國標准。C++運行期庫提供了以"str"開頭的一些列的函數操作此字符集。
3、C++運行期庫為Unicode字符集提供了一系列以"wcs"開頭的函數。
二、對應的數據類型
1、對於ANSI字符定義為char。
2、對於Unicode的字符定義為wchar_t。
三、使用環境
1、首先要說明的是Win98對於Unicode的支持是很微弱的,所以如果要在Win98上運行Unicode編譯的程序,可能造成運行錯誤或者失敗。
2、 由於Win2000及以后的OS的內核都是使用Unicode編寫的,所以雖然可以在其上運行ANSI編碼的程序,但是其運行過程中很多地方都需要將 ANSI轉換為Unicode以后,調用Unicode版本的函數,因為這個轉換的過程存在所以ANSI的程序運行效率不高。在Win2000上最好使用 Unicode編寫程序。
四、編寫通用的程序
1、在編程的時候使用TCHAR數據類型,此類型能夠根據預編譯宏的定義,將其轉換為ANSI或者是Unicode。
2、預編譯宏_MBCS、_UNICODE和UNICODE。_MBCS是多字節和ANSI字符串的編譯宏。此時TCHAR將轉換為char。_UNICODE和UNICODE是Unicode編碼的預編譯宏,TCHAR將轉換為wchar_t。
3、_UNICODE和UNICODE與_MBCS不能在編譯的時候同時被定義。
4、_UNICODE宏用於C運行期庫的頭文件,UNICODE宏用於Windows頭文件。一般同時定義這兩個宏。
五、轉換函數
1、Unicode轉換為ANSI使用:MultiByteToWideChar。
2、ANSI轉換為Unicode使用:WideCharToMultiByte。
寬字符轉多字符: size_t wcstombs(char *mbstr, const wchar_t *wcstr, size_t count ); 多字符轉寬字符: size_t mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count ); 另:L"ab"是C/C++標准宏,使用上是沒有問題的 |
1、client 里有些函數接口需要unicode,這些由於資源也在本地,可以直接使用MultiByteToWideChar或者mbstowcs+setlocale 轉換
2、對於需要從 中文client->服務器->韓文client的方式下,在傳文本的情況下,需要將文字的語言代碼一起傳出去,在接受端可以使用指定的代 碼,轉換。服務器如有必要的話,也可以使用該代碼轉換,這樣就可以在client上同時顯示多國語言了