string s = "おはよう";
wstring ws = FUNCTION(s, ws);

如何将s的内容分配给ws?

搜索谷歌并使用了一些技术,但他们不能分配确切的内容。内容被扭曲了。


当前回答

使用Boost。地区:

ws = boost::locale::conv::utf_to_utf<wchar_t>(s);

其他回答

string s =“早上好”;is an error。

你应该直接使用wstring:

wstring ws = L"おはよう";

您可以使用boost路径或std路径;这样就简单多了。 Boost路径更容易用于跨平台应用程序

#include <boost/filesystem/path.hpp>

namespace fs = boost::filesystem;

//s to w
std::string s = "xxx";
auto w = fs::path(s).wstring();

//w to s
std::wstring w = L"xxx";
auto s = fs::path(w).string();

如果你喜欢使用std:

#include <filesystem>
namespace fs = std::filesystem;

//The same

c++旧版本

#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;

//The same

代码内仍然实现了一个转换器,你不必解开细节。

int StringToWString(std::wstring &ws, const std::string &s)
{
    std::wstring wsTmp(s.begin(), s.end());

    ws = wsTmp;

    return 0;
}

使用Boost。地区:

ws = boost::locale::conv::utf_to_utf<wchar_t>(s);

对我来说,最简单又没有大开销的选择是:

包括:

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

转换:

char* whatever = "test1234";
std::wstring lwhatever = std::wstring(CA2W(std::string(whatever).c_str()));

如果需要:

lwhatever.c_str();