C++标准中,wchar_t是宽字符类型,每个wchar_t类型占2个字节,长度为16位。汉字的表示就要用到wchar_t
。char,我们都知道,占一个字节,长度为8位。所以在转换的时候将wchar_t用2个char字符来存储。当遇到ASCII时候就不用转换,具体的实现看下面W2C和wstr2cstr函数。
#define
ASCII_CHAR 1
#define
WIDE_CHAR 2
int
W2C(const wchar_t
*pw,char *pc)
{
int ichar_wide=0;
if (*pw > 0 &&
*pw <
256)
{
*pc = *pw;
ichar_wide = ASCII_CHAR;
}
else
{
*pc++ = *pw>>8;
*pc = *pw;
ichar_wide = WIDE_CHAR;
}
return ichar_wide;
}
int
C2W(const char *
pc,wchar_t *pw)
{
int ichar_wide=0;
if (*pc > 0 &&
*pc <
256)
{
*pw = *pc;
ichar_wide = ASCII_CHAR;
}
else{
char c1 = *pc;
*pc++;
char c2 = *pc;
wchar_t wc;
wc = (c1<<8)&0xff00;
wc |= ((c2)&0x00ff);
*pw = wc;
ichar_wide = WIDE_CHAR;
}
return
ichar_wide;
}
char
* wstr2cstr(const wchar_t*
pwstr,char *pcstr,size_t len)
{
char *ptemp = pcstr;
if (pwstr != 0 && pcstr != 0)
{
size_t wstr_len = wcslen(pwstr);
len = (len>wstr_len)?wstr_len:len;
while (len-->0)
{
int wide = W2C(pwstr,pcstr);
pwstr++;
pcstr += wide;
}
*pcstr='\0';
return ptemp;
}
return 0;
}
wchar_t
* cstr2wstr(const char*
pcstr,wchar_t *pwstr,size_t len)
{
wchar_t * ptemp = pwstr;
if (pcstr != 0 && pwstr != 0)
{
size_t cstr_len = strlen(pcstr);
len = (len < cstr_len)?cstr_len:len;
while(len-- > 0)
{
int wide = C2W(pcstr,pwstr);
pwstr++;
pcstr += wide;
}
*pwstr = '\0';
return ptemp;
}
return 0;
}
下面是测试代码:
int
main(void)
{
wchar_t
pwstr[] = {'你','好','A','中','国','\0'};
char *pcstr = (char*)new
char[2*wcslen(pwstr)+1];
memset(pcstr,0,2*wcslen(pwstr)+1);
wstr2cstr(pwstr,pcstr,wcslen(pwstr));
char *pc="你好Aцさ中国";
wchar_t *pw = (wchar_t*)new
wchar_t[(strlen(pc)/2)+2];
cstr2wstr(pc,pw,(strlen(pc)/2));
int len = wcslen(pw);
char *pc2 = (char*)new
char[2*len+1];
memset(pc2,0,2*len+1);
wstr2cstr(pw,pc2,len);
return
0;
}