Conversion of wchar_t* to string

Anas90 picture Anas90 · Dec 31, 2014 · Viewed 98.9k times · Source

How can I convert an wchar_t* array to an std::string varStr in win32 console.

Answer

FelipeDurar picture FelipeDurar · Dec 31, 2014

Use wstring, see this code:

// Your wchar_t*
wchar_t* txt = L"Hello World";
wstring ws(txt);
// your new String
string str(ws.begin(), ws.end());
// Show String
cout << str << endl;