0

consider the following piece of code:

#include <iostream>
#include <string>
#include <codecvt>

std::wstring string_to_wstring(const std::string& str)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
    return converter.from_bytes(str);
}

int main()
{
    std::string str = "abcä"; // without the "ä" it works
    std::wstring wstr = string_to_wstring(str);
    std::wcout << wstr << L"\n";
}

This throws me a "bad_conversion" exception, which seems to be caused by the umlaut because if I remove the "ä", everything works.

I have found the code for the string_to_wstring function some time ago here on SO and it worked until now very well. Mainly because I never came accross any umlauts.

Can we fix this function to work with any characters? Or is there a better (more efficient/safe) way to convert between string and wstring?

Community
  • 1
  • 1
SampleTime
  • 291
  • 3
  • 19
  • The standard says that a string literal is: _"Narrow multibyte string literal_" not UTF-8. Try prefixing with __u8__ as eg `std::string str = u8"abcä";` see: http://en.cppreference.com/w/cpp/language/string_literal – Richard Critten May 04 '17 at 21:30
  • That won't help unfortunatly because the same problem apears if I don't hardcode the string into the code but read it for example from a textfile (in my app this textfile is provided by the users so I can't assume correct encoding). Isn't there a reliable way to do this regardless of the used encoding? – SampleTime May 05 '17 at 21:36
  • Users provider text files that are not in UTF-8? They are either time travelers from 1994 or Windows users. For Windows, use MultiByteToWideChar – Cubbi May 08 '17 at 13:02
  • MultiByteToWideChar is however not portable unfortunatly... so it seems there is (still) no portable solution to this available? – SampleTime May 08 '17 at 16:38
  • @SampleTime UTF-8 is the solution. – Cubbi May 26 '17 at 10:27
  • You have read that this has to work for non UTF-8 textfiles, so no, UTF-8 is not the solution since I can't assume that encoding. And using MultiByteToWideChar for Windows defeats the purpose of having a std solution in the first place, since std solutions should be portable. – SampleTime Jun 08 '17 at 20:24

0 Answers0