Converting Utf-8

Oct 8, 2015 at 11:15 AM

I need help with creating json::value containing non-ASCII characters:
string xx = "Čč";
json::value v = json::value(conversions::to_string_t(xx));
I am getting Exception - "UTF-8 continuation byte is missing leading byte".

What is the right way to convert strings like that?
Oct 8, 2015 at 1:32 PM
Edited Oct 8, 2015 at 1:34 PM
I had to implement my own conversion:
string str = "Čč"; //not UTF-8

std::wstring convertedString;
//CP_THREAD_ACP - "The Windows ANSI code page for the current thread."
int requiredSize = MultiByteToWideChar(CP_THREAD_ACP, 0, str.c_str(), -1, 0, 0); 
if (requiredSize > 0)
    std::vector<wchar_t> buffer(requiredSize);
    MultiByteToWideChar(CP_THREAD_ACP, 0, str.c_str(), -1, &buffer[0], requiredSize);
    convertedString.assign(buffer.begin(), buffer.end() - 1);

json::value v = json::value(convertedString);
Is this the recommended practice or am I missing something?
Marked as answer by markyp82 on 10/9/2015 at 12:39 AM
Oct 8, 2015 at 7:39 PM
Edited Oct 8, 2015 at 7:39 PM
If you must support codepages, then your approach seems correct. However, if at all possible, I recommend using UTF-8 or UTF-16. We have conversion functions available in the library[1] and your code will be more portable in general.

Marked as answer by markyp82 on 10/9/2015 at 12:39 AM