将char *转换为wchar * [英] Convert char * to wchar*

查看:71
本文介绍了将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?

推荐答案

例如,这种方式:
for instance, this way:
  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:\\";



?



?


如果您100%确定您的char*字符串仅是ASCII,则加宽"最快,最简单的方法就是:

If you are 100% sure your char* string is ASCII only, the fastest and easiest way to "widen" it would be something like:

std::wstring w;
std::copy(p, p + strlen(p), back_inserter(w));
const WCHAR *pwcsName = w.c_str();


要转换为unicode,您需要使用以下函数:

MultiByteToWideChar->

http://msdn.microsoft.com/en-us/library/dd319072 (v = vs.85).aspx [
To Convert to unicode you need to use the following function:

MultiByteToWideChar -->

http://msdn.microsoft.com/en-us/library/dd319072(v=vs.85).aspx[^]

example of MultiByteToWideChar:

MultiByteToWideChar(CP_UTF8, 0, buf, -1 , NULL, 0);


这篇关于将char *转换为wchar *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆