Easiest way to convert UnicodeString to const char* in c++?

user2309974 picture user2309974 · Apr 23, 2013 · Viewed 8.1k times · Source

I'm new in c++ and have problem with converting UnicodeString to string, so now searching for easiest method to convert from one type to other.

I want to use basic windows function which needs string with UnicodeString, how to make code work?

UnicodeString Exec = "notepad";
WinExec(Exec.c_str(), 0);

Environment used is c++ builder xe2

Answer

Salgar picture Salgar · Apr 23, 2013

A std::string can not store unicode data. You will need a std::wstring for that.

I've never heard of UnicodeString before, but looking at the API here:

http://docwiki.embarcadero.com/Libraries/XE2/en/System.UnicodeString_Methods

It has a function called .c_str() which returns a wchar_t* which you can then use to construct a std::wstring

If you really need a std::string, then have a look at this answer.

How to convert wstring into string?