OUString UTF8 Character encoding Conversion Cédric BOSDONNAT Howto convert an rtl::OUString into UTF-8 encoding?

The rtl::OUString stores the text in UTF-16, however a lot of applications are handling UTF-8. The problem is to convert each OUString into the right encoding

First thing to do is to fully understand what we are doing: what are we converting ? is this really UTF-16 (UCS-2) ? The code shows how to create a correct string.

The important trick is to use the rtl::OUStringToOString() function with the RTL_TEXTENCODING_UTF8 flag. Have a look at the code: a piece of code if much better than a long text ;)

/* Creation of a test OUString with extended ascii letters createFromAscii converts only ascii characters between 0 and 127: that's why the first string will get wrongly encoded. The second way of creating the OUString is correct: the length of the string have to be provided with it's encoding. All the encoding flags can be found in rtl/textenc.h */ OUString sWrongString = OUSTring::createFromAscii("très"); OUString sGoodString = OUString("très", 4, RTL_TEXTENCODING_ISO_8859_15); /* Convertion of the OUString into an OString encoded in UTF-8 */ OString sUtf8String = OUStringToOString(sGoodString, RTL_TEXTENCODING_UTF8);