我想将 std::string 转换为 const wchar_t *
有什么方法吗? 我的电脑是AMD64。
::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);
当我使用:
loadU(&str);
VS2005编译器说:
Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *'
How can I do it?
Is there any method?
My computer is AMD64.
::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);
When I used:
loadU(&str);
the VS2005 compiler says:
Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *'
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先将其转换为 std::wstring:
然后获取 C 字符串:
这仅适用于 ASCII 字符串,但如果底层字符串是 UTF-8 编码的,则它将不起作用。 使用 MultiByteToWideChar() 等转换例程可确保正确处理这种情况。
First convert it to std::wstring:
Then get the C string:
This only works for ASCII strings, but it will not work if the underlying string is UTF-8 encoded. Using a conversion routine like MultiByteToWideChar() ensures that this scenario is handled properly.
如果您有一个 std::wstring 对象,则可以对其调用
c_str()
来获取wchar_t*
:但是,由于您正在操作窄字符串,因此您首先需要扩大它。 这里有多种选择; 一种是使用 Windows 内置的
MultiByteToWideChar
例程。 这将为您提供一个LPWSTR
,它相当于wchar_t*
。If you have a std::wstring object, you can call
c_str()
on it to get awchar_t*
:Since you are operating on a narrow string, however, you would first need to widen it. There are various options here; one is to use Windows' built-in
MultiByteToWideChar
routine. That will give you anLPWSTR
, which is equivalent towchar_t*
.您可以使用 ATL 文本转换宏将窄 (char) 字符串转换为宽 (wchar_t) 字符串。 例如,要转换 std::string:
您还可以指定代码页,因此如果您的 std::string 包含 UTF-8 字符,您可以使用:
非常有用,但仅限 Windows。
You can use the ATL text conversion macros to convert a narrow (char) string to a wide (wchar_t) one. For example, to convert a std::string:
You can also specify a code page, so if your std::string contains UTF-8 chars you can use:
Very useful but Windows only.
如果您使用的是 Linux/Unix,请查看 GNU C(来自 ISO C 90)中定义的 mbstowcs() 和 wcstombs()。
mbs 代表“多字节字符串”,基本上是常见的以零结尾的 C 字符串。
wcs 代表宽字符字符串,是一个 wchar_t 数组。
有关宽字符的更多背景详细信息,请参阅 此处 的 glibc 文档。
If you are on Linux/Unix have a look at mbstowcs() and wcstombs() defined in GNU C (from ISO C 90).
mbs stand for "Multi Bytes String" and is basically the usual zero terminated C string.
wcs stand for Wide Char String and is an array of wchar_t.
For more background details on wide chars have a look at glibc documentation here.
需要将 wchar_t 字符串传递给函数,并且首先能够从与整数变量连接的文字字符串创建该字符串。
原始字符串如下所示,其中 4 是物理驱动器号,但我希望可以更改它以匹配我想要传递给函数的任何驱动器号
以下工作
Need to pass a wchar_t string to a function and first be able to create the string from a literal string concantenated with an integer variable.
The original string looks like this, where 4 is the physical drive number, but I want that to be changeable to match whatever drive number I want to pass to the function
The following works
如果可以使用
CString
,利用CString::GetString()
会很方便。例如在Windows中使用MFC,
If one can use
CString
, leveragingCString::GetString()
would be convenient.E.g. using MFC in Windows,