VC++ 2009-07-24 15:16:01 閱讀2388 評(píng)論0 字號(hào):大中小 訂閱
利用widechartomultibyte來(lái)轉(zhuǎn)換的函數(shù)
通常適合于window平臺(tái)上使用
#include <tchar.h>
#include <windows.h>
int _tmain(int argc, _tchar* argv[])
{
wchar_t pwstr[] =l"我是中國(guó)人";
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* 轉(zhuǎn)成char*的實(shí)現(xiàn)函數(shù)如下:
char *w2c(char *pcstr,const wchar_t *pwstr, size_t len)
{
int nlength=wcslen(pwstr);
//獲取轉(zhuǎn)換后的長(zhǎng)度
int nbytes = WideCharToMultiByte( 0, // specify the co
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;
// 通過(guò)以上得到的結(jié)果,轉(zhuǎn)換unicode 字符為ascii 字符
WideCharToMultiByte( 0, // specify the co
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* 轉(zhuǎn)成wchar_t*的實(shí)現(xiàn)函數(shù)如下:
//這是把a(bǔ)sii字符轉(zhuǎn)換為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;
}
}
或者用此種方法更好一些:============我自已做的
//把a(bǔ)scii 字符轉(zhuǎn)換為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;
}
相關(guān)知識(shí)點(diǎn):
Unicode的出現(xiàn)是為了適應(yīng)軟件國(guó)際化的需要。Unicode不同于雙字節(jié)字符集(DBCS)。
一、相關(guān)操作函數(shù)
1、DBCS使用下面的函數(shù)操作字符串:
CharNext——獲得后一個(gè)字符
CharPrev——獲得前一個(gè)字符
IsDBCSLeadByte——判斷是否為兩個(gè)字節(jié)字符的第一個(gè)字節(jié)
C++運(yùn)行期庫(kù)提供了以"_mbs"開(kāi)頭的一系列的函數(shù)操作DBCS。類(lèi)似的函數(shù)有_mbscat等。
2、ANSI字符集是一個(gè)美國(guó)標(biāo)準(zhǔn)。C++運(yùn)行期庫(kù)提供了以"str"開(kāi)頭的一些列的函數(shù)操作此字符集。
3、C++運(yùn)行期庫(kù)為Unicode字符集提供了一系列以"wcs"開(kāi)頭的函數(shù)。
二、對(duì)應(yīng)的數(shù)據(jù)類(lèi)型
1、對(duì)于ANSI字符定義為char。
2、對(duì)于Unicode的字符定義為wchar_t。
三、使用環(huán)境
1、首先要說(shuō)明的是Win98對(duì)于Unicode的支持是很微弱的,所以如果要在Win98上運(yùn)行Unicode編譯的程序,可能造成運(yùn)行錯(cuò)誤或者失敗。
2、由于Win2000及以后的OS的內(nèi)核都是使用Unicode編寫(xiě)的,所以雖然可以在其上運(yùn)行ANSI編碼的程序,但是其運(yùn)行過(guò)程中很多地方都需要將ANSI轉(zhuǎn)換為Unicode以后,調(diào)用Unicode版本的函數(shù),因?yàn)檫@個(gè)轉(zhuǎn)換的過(guò)程存在所以ANSI的程序運(yùn)行效率不高。在Win2000上最好使用Unicode編寫(xiě)程序。
四、編寫(xiě)通用的程序
1、在編程的時(shí)候使用TCHAR數(shù)據(jù)類(lèi)型,此類(lèi)型能夠根據(jù)預(yù)編譯宏的定義,將其轉(zhuǎn)換為ANSI或者是Unicode。
2、預(yù)編譯宏_MBCS、_UNICODE和UNICODE。_MBCS是多字節(jié)和ANSI字符串的編譯宏。此時(shí)TCHAR將轉(zhuǎn)換為char。_UNICODE和UNICODE是Unicode編碼的預(yù)編譯宏,TCHAR將轉(zhuǎn)換為wchar_t。
3、_UNICODE和UNICODE與_MBCS不能在編譯的時(shí)候同時(shí)被定義。
4、_UNICODE宏用于C運(yùn)行期庫(kù)的頭文件,UNICODE宏用于Windows頭文件。一般同時(shí)定義這兩個(gè)宏。
五、轉(zhuǎn)換函數(shù)
1、Unicode轉(zhuǎn)換為ANSI使用:MultiByteToWideChar。
2、ANSI轉(zhuǎn)換為Unicode使用:WideCharToMultiByte。
寬字符轉(zhuǎn)多字符: size_t wcstombs(char *mbstr, const wchar_t *wcstr, size_t count ); 多字符轉(zhuǎn)寬字符: size_t mbstowcs(wchar_t *wcstr, const char *mbstr, size_t count ); 另:L"ab"是C/C++標(biāo)準(zhǔn)宏,使用上是沒(méi)有問(wèn)題的
|
1、client 里有些函數(shù)接口需要unicode,這些由于資源也在本地,可以直接使用MultiByteToWideChar或者mbstowcs+setlocale 轉(zhuǎn)換
2、對(duì)于需要從 中文client->服務(wù)器->韓文client的方式下,在傳文本的情況下,需要將文字的語(yǔ)言代碼一起傳出去,在接受端可以使用指定的代碼,轉(zhuǎn)換。服務(wù)器如有必要的話,也可以使用該代碼轉(zhuǎn)換,這樣就可以在client上同時(shí)顯示多國(guó)語(yǔ)言了
聯(lián)系客服