//将char*转化为whcar_t*,其中lpwchartStr需要释放
int charTowchar_t(wchar_t** lpwchartStr,char* lpcharStr,int
*len)
{
wchar_t* buffer = nullptr;
if(lpcharStr)
{
int nu = strlen(lpcharStr);
int n = MultiByteToWideChar(CP_ACP,0,(const char
*)lpcharStr,int(nu),NULL,0);
buffer =0;
buffer = new wchar_t[n+1];
//if(n>=len) n=len-1;
::MultiByteToWideChar(CP_ACP,0,(const char
*)lpcharStr,int(nu),buffer,int(n));
buffer[n] = 0;
*lpwchartStr = buffer;
*len = n;
return 0;
}
*len=0;
return -1;
}
用完用delete释放lpwchartStr。
//将wchar_t* 转成char*,其中lpcharStr需要释放
int wchar_tTochar(char** lpcharStr,wchar_t* lpwchartStr,int
*len)
{
if (!lpwchartStr)
{
*len=0;
return -1;
}
char *buffer = nullptr;
int nlength=wcslen(lpwchartStr);
//获取转换后的长度
int nbytes = WideCharToMultiByte( 0, // specify the code page
used to perform the conversion
0, // no special flags to handle unmapped
characters
lpwchartStr, // 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
buffer = new char[nbytes+1];
WideCharToMultiByte( 0, // specify the code page used to
perform the conversion
0, // no special flags to handle unmapped
characters
lpwchartStr, // wide character string to
convert
nlength, // the number of wide characters
in that string
buffer, // 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 );
buffer[nbytes]=0;
*lpcharStr = buffer;
*len = nbytes;
return 0;
}
用完之后记得delete释放lpcharStr;
---------------------------------------------------------------------------------
下面一部分引自“http://blog.csdn.net/oceanwavewyt/article/details/5612392”
汉字和ascii不同,汉字是多字节编码方式,ascii只用一个字节表示,所以在用c语言提供的strlen函数得到的长度是字节长度;以下都是utf-8编码方式,为了得到一个字符(汉字和ascii)有几种处理方式:
1. 自己写程序判断是否为汉字,通过第一个字节判断,以下是一部分代码:
int isHanzi(int a)
{
int t = a &
0xE0;
if(t==0xE0){
printf("汉字1/n");
return 3;
}
t = a &
0xC0;
if(t==0xC0){
return 2;
}
t = a &
0xF0;
if(t==0xF0){
return 4;
}
t = a &
0xF8;
if(t==0xF8){
return 5;
}
t = a &
0xFC;
if(t==0xFC){
return 2;
}
printf("不是/n");
return 1;
}
2. 用内置的wchar_t类型
1). wchar_t ws[]=L"北京bus";
这样用wcslen(ws)结果就为 5,
int i;
setlocale(LC_ALL, "zh_CN.UTF-8");
int
len=wcslen(ws);
for(i=0; i
printf("%lc/n",ws[i]);
}
这是必须设置
本地编码方式 setlocale(LC_ALL,"zh_CN.UTF-8");
2).读一个文件后得到的数据,要使数据为wchar_t
,就需要进行字符串转换了.用到函数 mbstowcs,以下是我写的一个读文本文 件然后转换的程序
//读一个文本文件
FILE
*fp = fopen("/home/wyt/data.txt","r");
char
buffer[1024];
size_t realLen=0,curLen,length=2048;
char
*data=(char *)malloc(sizeof(char)*length);
char
*tmp;
while(!feof(fp)){
curLen = fread(buffer,sizeof(char),1024,fp);
realLen+=curLen;
if(length<=realLen){
tmp =
(char *)malloc(sizeof(char)*(length+2048));
memcpy(tmp,data,sizeof(char)*length);
length+=2048;
free(data);
data=tmp;
}
strcat(data,buffer);
}
data[realLen]='/0';
printf("%s",data);
fclose(fp);
wchar_t *data2 = (wchar_t
*)malloc(sizeof(wchar_t)*length);
//转换成wchar_t类型
mbstowcs(data2,data,length);
size_t ln=wcslen(data2);
for(i=0;i
printf("%lc/n",data2[i]);
}
如果
wchar_t 转换为char 类型,可以用函数 wcstombs
void out(wchar_t name)
{
if(name<128){
char n[2];
wcstombs(n,&name,sizeof(wchar_t));
n[1]='/0';
printf("%lc,%s,%d/n",name,n,strlen(n));
}else{
char n[3];
wcstombs(n,&name,sizeof(wchar_t));
n[3]='/0';
printf("%lc,%s,%d/n",name,n,strlen(n));
}
//n[3]='/0';
}