Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a char string to a wchar_t string?

I have a string in char* format and would like to convert it to wchar_t*, to pass to a Windows function.

like image 353
justinhj Avatar asked Nov 24 '09 17:11

justinhj


2 Answers

Does this little function help?

#include <cstdlib>

int mbstowcs(wchar_t *out, const char *in, size_t size);

Also see the C++ reference

like image 109
schnaader Avatar answered Oct 05 '22 23:10

schnaader


If you don't want to link against the C runtime library, use the MultiByteToWideChar API call, e.g:

const size_t WCHARBUF = 100;
const char szSource[] = "HELLO";
wchar_t  wszDest[WCHARBUF];
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, szSource, -1, wszDest, WCHARBUF);
like image 41
Hernán Avatar answered Oct 05 '22 22:10

Hernán