Etan
Etan

Reputation: 17544

What is the easiest way to convert a char array to a WCHAR array?

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)

Upvotes: 0

Views: 1825

Answers (2)

Alexey Malistov
Alexey Malistov

Reputation: 26975

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];

Upvotes: 3

Goz
Goz

Reputation: 62323

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.

Upvotes: 3

Related Questions