Question

In my code, I receive a const char array like the following:

const char * myString = someFunction();

Now I want to postprocess it as a wchar array since the functions I use afterwards don't handle narrow strings.

What is the easiest way to accomplish this goal?

Eventually MultiByteToWideChar? (However, since it is a narrow string which I get as input, it doesn't have multibyte characters => probably not the most beautiful solution)

Was it helpful?

Solution

const char * myString = someFunction();
const int len = strlen(myString);
std::vector<wchar_t> myWString (len);
std::copy(myString, myString + len, myWString.begin());
const wchar_t * result = &myWString[0];

OTHER TIPS

MultiByteToWideChar will work unless you are using extended characters in your narrow string. If its a plain alpha numeric string then it should work fine.

you can also look at mbstowcs which is a little less convoluted but doesn't offer the same amount of control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top