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

后端 未结 16 2180
难免孤独
难免孤独 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:30

    Windows API only, pre C++11 implementation, in case someone needs it:

    #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);
    }
    
    0 讨论(0)
  • 2020-11-22 06:30

    using Boost.Locale:

    ws = boost::locale::conv::utf_to_utf<wchar_t>(s);
    
    0 讨论(0)
  • 2020-11-22 06:31
    int StringToWString(std::wstring &ws, const std::string &s)
    {
        std::wstring wsTmp(s.begin(), s.end());
    
        ws = wsTmp;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 06:32

    If you are using Windows/Visual Studio and need to convert a string to wstring you could use:

    #include <AtlBase.h>
    #include <atlconv.h>
    ...
    string s = "some string";
    CA2W ca2w(s.c_str());
    wstring w = ca2w;
    printf("%s = %ls", s.c_str(), w.c_str());
    

    Same procedure for converting a wstring to string (sometimes you will need to specify a codepage):

    #include <AtlBase.h>
    #include <atlconv.h>
    ...
    wstring w = L"some wstring";
    CW2A cw2a(w.c_str());
    string s = cw2a;
    printf("%s = %ls", s.c_str(), w.c_str());
    

    You could specify a codepage and even UTF8 (that's pretty nice when working with JNI/Java). A standard way of converting a std::wstring to utf8 std::string is showed in this answer.

    // 
    // using ATL
    CA2W ca2w(str, CP_UTF8);
    
    // 
    // or the standard way taken from the answer above
    #include <codecvt>
    #include <string>
    
    // convert UTF-8 string to wstring
    std::wstring utf8_to_wstring (const std::string& str) {
        std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
        return myconv.from_bytes(str);
    }
    
    // convert wstring to UTF-8 string
    std::string wstring_to_utf8 (const std::wstring& str) {
        std::wstring_convert<std::codecvt_utf8<wchar_t>> myconv;
        return myconv.to_bytes(str);
    }
    

    If you want to know more about codepages there is an interesting article on Joel on Software: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets.

    These CA2W (Convert Ansi to Wide=unicode) macros are part of ATL and MFC String Conversion Macros, samples included.

    Sometimes you will need to disable the security warning #4995', I don't know of other workaround (to me it happen when I compiled for WindowsXp in VS2012).

    #pragma warning(push)
    #pragma warning(disable: 4995)
    #include <AtlBase.h>
    #include <atlconv.h>
    #pragma warning(pop)
    

    Edit: Well, according to this article the article by Joel appears to be: "while entertaining, it is pretty light on actual technical details". Article: What Every Programmer Absolutely, Positively Needs To Know About Encoding And Character Sets To Work With Text.

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

    This variant of it is my favourite in real life. It converts the input, if it is valid UTF-8, to the respective wstring. If the input is corrupted, the wstring is constructed out of the single bytes. This is extremely helpful if you cannot really be sure about the quality of your input data.

    std::wstring convert(const std::string& input)
    {
        try
        {
            std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
            return converter.from_bytes(input);
        }
        catch(std::range_error& e)
        {
            size_t length = input.length();
            std::wstring result;
            result.reserve(length);
            for(size_t i = 0; i < length; i++)
            {
                result.push_back(input[i] & 0xFF);
            }
            return result;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:35

    String to 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 to 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);
    }
    
    0 讨论(0)
提交回复
热议问题