Converting wxString to C string const char*

wxWidgets is an extensive, free, open-source, and mature cross-platform graphical toolkit. Formerly known as wxWindows, wxWidgets applications look and feel just like a native application, since wxWidgets uses the native graphical toolkit for the target platform it is compiled for.

wxWidgets has an impressive number of features, which makes it an excellent choice for cross platform graphical application development.

One useful feature is support for Unicode. wxWidgets provides a wxString class which abstracts away from standard ASCII encoded strings and Unicode encoded strings.

However, converting between the two strings representations can be a bit cumbersome and confusing. After spending some time trying to figure out way every wxString that I converted to normal string was truncated at the very first character, I stumbled on the excellent wxString documentation page and wxString wiki page.

// Converting a wxString to a normal string
wxString string(_T("Some text"));
const char* ascii_str = (const char*)string.mb_str(wxConvUTF8);

// Converting a normal string to a wxString
const char* ascii_str = "Some text";
wxString string(ascii_str, wxConvUTF8);

The official website has plenty more information.