UnicodeString to char* (UTF-8) UnicodeString to char* (UTF-8) windows windows

UnicodeString to char* (UTF-8)


call UnicodeString::extract(...) to extract into a char*, pass NULL for the converter to get the default converter (which is in the charset which your OS will be using).


ICU User Guide > UTF-8 provides methods and descriptions of doing that.

The simplest way to use UTF-8 strings in UTF-16 APIs is via the C++ icu::UnicodeString methods fromUTF8(const StringPiece &utf8) and toUTF8String(StringClass &result). There is also toUTF8(ByteSink &sink).

And extract() is not prefered now.

Note: icu::UnicodeString has constructors, setTo() and extract() methods which take either a converter object or a charset name. These can be used for UTF-8, but are not as efficient or convenient as the fromUTF8()/toUTF8()/toUTF8String() methods mentioned above.


This will work:

std::string utf8;uStr.toUTF8String(utf8);