mocode9 mocode9 - 7 months ago 45
reST (reStructuredText) Question

C++ Convert wstring to Unicode

I'm trying to use the rest API and one of the functions has an example like this:

uri_builder builder(U("/test.php"));
builder.append_query(U("worker"), U("hello"));

I want to use a variable instead of the string literal.
For now I have a std::wstring variable that I want to use but I can't figure out how to use it with the U (unsigned) prefix working on it.

Any suggestions?

I've tried

std::wstring name = L"the_name";
builder.append_query(U("worker"), U(the_name.c_str()));

I've also tried

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> convert;
std::string uWorkerName = convert.to_bytes(the_name);

My latest failed attempt

std::u32string uWorkerName(workerName.begin(), workerName.end());

The last one allowed me to use the API but when the data hit my php page it displayed garbage data such as random numbers and capital letters.


If I am not mistaken your rest api docs are under this url:

as you can see above, this namespace : utility::conversions, proivides various conversion functions which should be of use to you. Example:

std::string name = "the_name";
builder.append_query(U("worker"), utility::conversions::to_string_t(name));

If you want your name to be composed of wchar_t characters, then above namespace contains also overloads for utf16string arguments. From this site:

You can learn that utf16string always tries to be string of two byte characters. Under windows it should be typedef std::wstring utf16string; while on linux-es probably typedef std::u16string utf16string;.