本文介绍了将char *转换为wchar *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将字符串转换为Unicode字符串
I am trying to convert a string to a Unicode String
char *p="D:\";
const WCHAR *pwcsName;
现在我想将p(char *)
转换为pwcsName(WCHAR *)
.
有人可以建议我怎么做吗?
使用"mbtowc"函数还是MultiByteToWideChar完成...
请给我建议代码吗?
Now I want convert p(char *)
to pwcsName(WCHAR *)
.
Can anybody suggest me how do this?
Is it done using "mbtowc" function or MultiByteToWideChar...
plz can someone suggest me code for that?
推荐答案
char *p="D:\\"; //just for proper syntax highlighting ..."
const WCHAR *pwcsName;
// required size
int nChars = MultiByteToWideChar(CP_ACP, 0, p, -1, NULL, 0);
// allocate it
pwcsName = new WCHAR[nChars];
MultiByteToWideChar(CP_ACP, 0, p, -1, (LPWSTR)pwcsName, nChars);
// use it....
// delete it
delete [] pwcsName;
}
但是,为什么不简单地做
However, why don''t you simply do
const WCHAR *pwcsName = L"D:\\";
?
?
std::wstring w;
std::copy(p, p + strlen(p), back_inserter(w));
const WCHAR *pwcsName = w.c_str();
MultiByteToWideChar(CP_UTF8, 0, buf, -1 , NULL, 0);
这篇关于将char *转换为wchar *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!