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

发布于 2024-07-08 06:01:08 字数 358 浏览 6 评论 0原文

有什么方法吗? 我的电脑是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪异←思 2024-07-15 06:01:08

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

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

然后获取 C 字符串:

const wchar_t* widecstr = widestr.c_str();

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

First convert it to std::wstring:

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

Then get the C string:

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.

请持续率性 2024-07-15 06:01:08

如果您有一个 std::wstring 对象,则可以对其调用 c_str() 来获取 wchar_t*

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

但是,由于您正在操作窄字符串,因此您首先需要扩大它。 这里有多种选择; 一种是使用 Windows 内置的 MultiByteToWideChar 例程。 这将为您提供一个LPWSTR,它相当于wchar_t*

If you have a std::wstring object, you can call c_str() on it to get a 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 MultiByteToWideChar routine. That will give you an LPWSTR, which is equivalent to wchar_t*.

爱格式化 2024-07-15 06:01:08

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

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

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

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

非常有用,但仅限 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:

#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:

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

Very useful but Windows only.

为人所爱 2024-07-15 06:01:08

如果您使用的是 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.

难以启齿的温柔 2024-07-15 06:01:08

需要将 wchar_t 字符串传递给函数,并且首先能够从与整数变量连接的文字字符串创建该字符串。

原始字符串如下所示,其中 4 是物理驱动器号,但我希望可以更改它以匹配我想要传递给函数的任何驱动器号

auto TargetDrive = L"\\\\.\\PhysicalDrive4";

以下工作

int a = 4;


std::string stddrivestring = "\\\\.\\PhysicalDrive" + to_string(a);

std::wstring widedrivestring = std::wstring(stddrivestring.begin(), stddrivestring.end());

const wchar_t* TargetDrive = widedrivestring.c_str();

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

auto TargetDrive = L"\\\\.\\PhysicalDrive4";

The following works

int a = 4;


std::string stddrivestring = "\\\\.\\PhysicalDrive" + to_string(a);

std::wstring widedrivestring = std::wstring(stddrivestring.begin(), stddrivestring.end());

const wchar_t* TargetDrive = widedrivestring.c_str();
鸠魁 2024-07-15 06:01:08

如果可以使用 CString,利用 CString::GetString() 会很方便。

    #include <atlstr.h> // to use CString.
    std::string s = "std::string 한글";
    std::wstring ws = static_cast<CString>(s.c_str()).GetString();

例如在Windows中使用MFC,

    std::string s = "std::string 한글";

    std::wstring ws1 = std::wstring(s.cbegin(), s.cend()); // Uh-oh.
    ::AfxMessageBox(ws1.c_str()); // does NOT print correctly.

    std::wstring ws2 = static_cast<CString>(s.c_str()).GetString();
    ::AfxMessageBox(ws2.c_str()); // prints correctly.
    
    // some more examples.
    ::AfxMessageBox(static_cast<CString>(s.c_str()).GetString()); // prints correctly. 
    ::AfxMessageBox(static_cast<CString>(s.c_str())); // Also works without GetString().
    
    // this is fine.
    std::wstring ws3 = L"std::wstring 한글";
    ::AfxMessageBox(ws3.c_str()); // prints correctly.

If one can use CString, leveraging CString::GetString() would be convenient.

    #include <atlstr.h> // to use CString.
    std::string s = "std::string 한글";
    std::wstring ws = static_cast<CString>(s.c_str()).GetString();

E.g. using MFC in Windows,

    std::string s = "std::string 한글";

    std::wstring ws1 = std::wstring(s.cbegin(), s.cend()); // Uh-oh.
    ::AfxMessageBox(ws1.c_str()); // does NOT print correctly.

    std::wstring ws2 = static_cast<CString>(s.c_str()).GetString();
    ::AfxMessageBox(ws2.c_str()); // prints correctly.
    
    // some more examples.
    ::AfxMessageBox(static_cast<CString>(s.c_str()).GetString()); // prints correctly. 
    ::AfxMessageBox(static_cast<CString>(s.c_str())); // Also works without GetString().
    
    // this is fine.
    std::wstring ws3 = L"std::wstring 한글";
    ::AfxMessageBox(ws3.c_str()); // prints correctly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文