 | OO-Snippets: OUString and UTF8Commons| Keywords | OUString, UTF8, Character encoding, Conversion |
|---|
| Language | Cpp |
|---|
| Application | Office |
|---|
| Authors | Cédric BOSDONNAT (initial)
|
|---|
| Supported Versions | 2.0.x |
|---|
| Supported OS | All |
|---|
| Question | 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
|
|---|
| Answer | 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 ;) |
|---|
OUString sWrongString = OUSTring::createFromAscii("très");
OUString sGoodString = OUString("très", 4, RTL_TEXTENCODING_ISO_8859_15);
OString sUtf8String = OUStringToOString(sGoodString, RTL_TEXTENCODING_UTF8);
|
|