1. 问题描述
编写程序时通常会面对一些不同的编码格式,例如把wchar*的字符串转换为char*的字符串,有时还需要把char*类型的字符串转换为wchar*类型。下面提供几种解决方案。
2. 解决方案
2.0 函数方法
//char convert to wchar_t
wchar_t* char2wchar_t(char* cstr){
int len = MultiByteToWideChar(CP_ACP,0,cstr,strlen(cstr),NULL,0);
wstr = new wchar_t[len+1];
MultiByteToWideChar(CP_ACP,0,cstr,strlen(cstr),wstr,len);
wstr[len] = '\0';
return wstr;
delete wstr;
}
//wchar_t convert to char
char* wchar_t2char(wchar_t* cstr){
int len = WideCharToMultiByte(CP_ACP,0,wstr,strlen(wstr),NULL,0,NULL,NULL);
cstr = new char[len+1];
WideCharToMultiByte(CP_ACP,0,wstr,strlen(wstr),cstr,len,NULL,NULL);
wstr[len] = '\0';
return cstr;
delete cstr;