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

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

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


当前回答

这是我的超级基本解决方案,可能并不适用于所有人。但对很多人都适用。

它需要使用指南支持库。 这是一个非常官方的c++库,由许多c++委员会的作者设计:

https://github.com/isocpp/CppCoreGuidelines https://github.com/Microsoft/GSL

    std::string to_string(std::wstring const & wStr)
    {
        std::string temp = {};

        for (wchar_t const & wCh : wStr)
        {
            // If the string can't be converted gsl::narrow will throw
            temp.push_back(gsl::narrow<char>(wCh));
        }

        return temp;
    }

我的函数所做的只是允许转换。否则抛出异常。

通过使用gsl::narrow (https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es49-if-you-must-use-a-cast-use-a-named-cast)

其他回答

从char*到wstring:

char* str = "hello worlddd";
wstring wstr (str, str+strlen(str));

从string到wstring:

string str = "hello worlddd";
wstring wstr (str.begin(), str.end());

注意,只有在被转换的字符串只包含ASCII字符时,这种方法才有效。

只有Windows API,前c++ 11实现,以防有人需要它:

#include <stdexcept>
#include <vector>
#include <windows.h>

using std::runtime_error;
using std::string;
using std::vector;
using std::wstring;

wstring utf8toUtf16(const string & str)
{
   if (str.empty())
      return wstring();

   size_t charsNeeded = ::MultiByteToWideChar(CP_UTF8, 0, 
      str.data(), (int)str.size(), NULL, 0);
   if (charsNeeded == 0)
      throw runtime_error("Failed converting UTF-8 string to UTF-16");

   vector<wchar_t> buffer(charsNeeded);
   int charsConverted = ::MultiByteToWideChar(CP_UTF8, 0, 
      str.data(), (int)str.size(), &buffer[0], buffer.size());
   if (charsConverted == 0)
      throw runtime_error("Failed converting UTF-8 string to UTF-16");

   return wstring(&buffer[0], charsConverted);
}

假设您的示例(おはよう)中的输入字符串是UTF-8编码的(从表面上看,它不是,但为了解释起见,让我们假设它是您感兴趣的Unicode字符串的表示形式:-),那么您的问题可以仅通过标准库(c++ 11或更新版本)完全解决。

TL;DR版本:

#include <locale>
#include <codecvt>
#include <string>

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string narrow = converter.to_bytes(wide_utf16_source_string);
std::wstring wide = converter.from_bytes(narrow_utf8_source_string);

更长的在线可编译和可运行的示例:

(它们都展示了相同的例子。只是有很多冗余……)

http://ideone.com/KA1oty http://ide.geeksforgeeks.org/5pRLSh http://rextester.com/DIJZK52174

备注(旧):

正如评论中指出并在https://stackoverflow.com/a/17106065/6345中解释的那样,在使用标准库在UTF-8和UTF-16之间进行转换时,在不同的平台上可能会产生意想不到的结果差异。为了更好的转换,请考虑http://en.cppreference.com/w/cpp/locale/codecvt_utf8上描述的std::codecvt_utf8

备注(新):

由于codecvt头文件在c++ 17中已弃用,因此有人对这个答案中提出的解决方案提出了一些担忧。然而,c++标准委员会在http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0618r0.html中增加了一项重要声明

这个库组件应该退役到附件D中,直到一个合适的替换被标准化。

所以在可预见的未来,这个答案中的codecvt解决方案是安全的和可移植的。

String到wstring

std::wstring Str2Wstr(const std::string& str)
{
    int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
    std::wstring wstrTo(size_needed, 0);
    MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
    return wstrTo;
}

从wstring到String

std::string Wstr2Str(const std::wstring& wstr)
{
    typedef std::codecvt_utf8<wchar_t> convert_typeX;
    std::wstring_convert<convert_typeX, wchar_t> converterX;
    return converterX.to_bytes(wstr);
}

这是我的超级基本解决方案,可能并不适用于所有人。但对很多人都适用。

它需要使用指南支持库。 这是一个非常官方的c++库,由许多c++委员会的作者设计:

https://github.com/isocpp/CppCoreGuidelines https://github.com/Microsoft/GSL

    std::string to_string(std::wstring const & wStr)
    {
        std::string temp = {};

        for (wchar_t const & wCh : wStr)
        {
            // If the string can't be converted gsl::narrow will throw
            temp.push_back(gsl::narrow<char>(wCh));
        }

        return temp;
    }

我的函数所做的只是允许转换。否则抛出异常。

通过使用gsl::narrow (https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es49-if-you-must-use-a-cast-use-a-named-cast)