C++ Convert string (or char*) to wstring (or wchar_t*)

后端 未结 16 2181
难免孤独
难免孤独 2020-11-22 05:57
string s = \"おはよう\";
wstring ws = FUNCTION(s, ws);

How would i assign the contents of s to ws?

Searched google and used some techniques but

相关标签:
16条回答
  • 2020-11-22 06:49

    std::string -> wchar_t[] with safe mbstowcs_s function:

    auto ws = std::make_unique<wchar_t[]>(s.size() + 1);
    mbstowcs_s(nullptr, ws.get(), s.size() + 1, s.c_str(), s.size());
    

    This is from my sample code

    0 讨论(0)
  • 2020-11-22 06:49

    string s = "おはよう"; is an error.

    You should use wstring directly:

    wstring ws = L"おはよう";
    
    0 讨论(0)
  • 2020-11-22 06:50

    From char* to wstring:

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

    From string to wstring:

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

    Note this only works well if the string being converted contains only ASCII characters.

    0 讨论(0)
  • 2020-11-22 06:52

    Assuming that the input string in your example (おはよう) is a UTF-8 encoded (which it isn't, by the looks of it, but let's assume it is for the sake of this explanation :-)) representation of a Unicode string of your interest, then your problem can be fully solved with the standard library (C++11 and newer) alone.

    The TL;DR version:

    #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);
    

    Longer online compilable and runnable example:

    (They all show the same example. There are just many for redundancy...)

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

    Note (old):

    As pointed out in the comments and explained in https://stackoverflow.com/a/17106065/6345 there are cases when using the standard library to convert between UTF-8 and UTF-16 might give unexpected differences in the results on different platforms. For a better conversion, consider std::codecvt_utf8 as described on http://en.cppreference.com/w/cpp/locale/codecvt_utf8

    Note (new):

    Since the codecvt header is deprecated in C++17, some worry about the solution presented in this answer were raised. However, the C++ standards committee added an important statement in http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0618r0.html saying

    this library component should be retired to Annex D, along side , until a suitable replacement is standardized.

    So in the foreseeable future, the codecvt solution in this answer is safe and portable.

    0 讨论(0)
提交回复
热议问题