简体   繁体   English

如何在 C++ (Unicode) 中将 std::string 转换为 LPCWSTR

[英]How to convert std::string to LPCWSTR in C++ (Unicode)

I'm looking for a method, or a code snippet for converting std::string to LPCWSTR我正在寻找将 std::string 转换为 LPCWSTR 的方法或代码片段

Thanks for the link to the MSDN article.感谢您提供 MSDN 文章的链接。 This is exactly what I was looking for.这正是我正在寻找的。

std::wstring s2ws(const std::string& s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

std::wstring stemp = s2ws(myString);
LPCWSTR result = stemp.c_str();

The solution is actually a lot easier than any of the other suggestions:该解决方案实际上比任何其他建议都容易得多:

std::wstring stemp = std::wstring(s.begin(), s.end());
LPCWSTR sw = stemp.c_str();

Best of all, it's platform independent.最重要的是,它是独立于平台的。

If you are in an ATL/MFC environment, You can use the ATL conversion macro:如果您在 ATL/MFC 环境中,您可以使用 ATL 转换宏:

#include <atlbase.h>
#include <atlconv.h>

. . .

string myStr("My string");
CA2W unicodeStr(myStr);

You can then use unicodeStr as an LPCWSTR.然后您可以使用 unicodeStr 作为 LPCWSTR。 The memory for the unicode string is created on the stack and released then the destructor for unicodeStr executes. unicode 字符串的内存在堆栈上创建并释放,然后执行 unicodeStr 的析构函数。

I prefer using standard converters:我更喜欢使用标准转换器:

#include <codecvt>

std::string s = "Hi";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wide = converter.from_bytes(s);
LPCWSTR result = wide.c_str();

Please find more details in this answer: https://stackoverflow.com/a/18597384/592651请在此答案中找到更多详细信息: https : //stackoverflow.com/a/18597384/592651


Update 12/21/2020 : My answer was commented on by @Andreas H . 2020 年 12 月 21 日更新:@Andreas H 评论了我的回答。 I thought his comment is valuable, so I updated my answer accordingly:我认为他的评论很有价值,所以我相应地更新了我的答案:

  1. codecvt_utf8_utf16 is deprecated in C++17. codecvt_utf8_utf16在 C++17 中已弃用。
  2. Also the code implies that source encoding is UTF-8 which it usually isn't.此外,代码暗示源编码是 UTF-8,但通常不是。
  3. In C++20 there is a separate type std::u8string for UTF-8 because of that.因此,在 C++20 中,UTF-8 有一个单独的类型 std::u8string。

But it worked for me because I am still using an old version of C++ and it happened that my source encoding was UTF-8 .但它对我有用,因为我仍在使用旧版本的 C++ 并且碰巧我的源编码是 UTF-8 。

如果您使用的是 QT,那么您可以转换为 QString,然后 myqstring.toStdWString() 就可以解决问题。

Same as Toran Billups's answer , except that we should know:Toran Billups 的回答相同,除了我们应该知道:

The C++-standard has rules for .c_str() method, which allows us to use const_cast (instead of the unnecessary allocation and deletion). C++ 标准对.c_str()方法有规则,它允许我们使用const_cast (而不是不必要的分配和删除)。

std::wstring s2ws(const std::string &s)
{
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    std::wstring buf;
    buf.resize(len);
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength,
           const_cast<wchar_t *>(buf.c_str()), len);
    return buf;
}

std::wstring wrapper = s2ws("My UTF8 string!");
LPCWSTR result = wrapper.c_str();

Instead of using a std::string, you could use a std::wstring.您可以使用 std::wstring 代替 std::string。

EDIT: Sorry this is not more explanatory, but I have to run.编辑:对不起,这不是更具解释性,但我必须运行。

Use std::wstring::c_str()使用 std::wstring::c_str()

It's so easy, no need to apply any custom method.这很简单,无需应用任何自定义方法。 Try with this:试试这个:

string s = "So Easy Bro"
LPCWSTR wide_string;

wide_string = CA2T(s.c_str());

I think, it will works.我认为,它会起作用。

string  myMessage="helloworld";
int len;
int slength = (int)myMessage.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, myMessage.c_str(), slength, 0, 0); 
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, myMessage.c_str(), slength, buf, len);
std::wstring r(buf);
 std::wstring stemp = r.C_str();
LPCWSTR result = stemp.c_str();

LPCWSTR lpcwName=std::wstring(strname.begin(), strname.end()).c_str()

the first answer worked for me:)第一个答案对我有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM