char 轉wchar_t 及wchar_t轉char

利用widechartomultibyte來轉換的函數

通常適合於window平臺上使用

#include <tchar.h>

#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。 


轉自:http://my.oschina.net/abcMx/blog/110169

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