C++ 我想将 std::string 转换为 const wchar_t *

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/246806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:02:12  来源:igfitidea点击:

I want to convert std::string into a const wchar_t *

c++stlwchar-tstdstring

提问by user25749

Is there any method? My computer is AMD64.

有什么方法吗?我的电脑是AMD64。

::std::string str;
BOOL loadU(const wchar_t* lpszPathName, int flag = 0);

When I used:

当我使用:

loadU(&str);

the VS2005 compiler says:

VS2005 编译器说:

Error 7 error C2664:: cannot convert parameter 1 from 'std::string *__w64 ' to 'const wchar_t *'

How can I do it?

我该怎么做?

采纳答案by Matt Dillard

If you have a std::wstring object, you can call c_str()on it to get a wchar_t*:

如果你有一个 std::wstring 对象,你可以调用c_str()它来获得一个wchar_t*

std::wstring name( L"Steve Nash" );
const wchar_t* szName = name.c_str();

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 MultiByteToWideCharroutine. That will give you an LPWSTR, which is equivalent to wchar_t*.

但是,由于您正在对窄字符串进行操作,因此您首先需要将其加宽。这里有多种选择;一种是使用Windows 的内置MultiByteToWideChar例程。这会给你一个LPWSTR,相当于wchar_t*

回答by marijne

First convert it to std::wstring:

首先将其转换为 std::wstring:

std::wstring widestr = std::wstring(str.begin(), str.end());

Then get the C string:

然后得到C字符串:

const wchar_t* widecstr = widestr.c_str();

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.

这仅适用于 ASCII 字符串,但如果底层字符串是 UTF-8 编码,则它将不起作用。使用 MultiByteToWideChar() 等转换例程可确保正确处理这种情况。

回答by Rob

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:

您可以使用 ATL 文本转换宏将窄 (char) 字符串转换为宽 (wchar_t) 字符串。例如,要转换 std::string:

#include <atlconv.h>
...
std::string str = "Hello, world!";
CA2W pszWide(str.c_str());
loadU(pszWide);

You can also specify a code page, so if your std::string contains UTF-8 chars you can use:

您还可以指定代码页,因此如果您的 std::string 包含 UTF-8 字符,您可以使用:

CA2W pszWide(str.c_str(), CP_UTF8);

Very useful but Windows only.

非常有用,但仅限 Windows。

回答by kriss

If you are on Linux/Unix have a look at mbstowcs() and wcstombs() defined in GNU C (from ISO C 90).

如果您使用的是 Linux/Unix,请查看 GNU C(来自 ISO C 90)中定义的 mbstowcs() 和 wcstombs()。

  • 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.

  • mbs 代表“多字节字符串”,基本上是通常的零终止 C 字符串。

  • wcs 代表宽字符字符串,是一个 wchar_t 数组。

For more background details on wide chars have a look at glibc documentation here.

有关宽字符的更多背景详细信息,请在此处查看 glibc 文档。