Hi there,

After a small discussion on the IRC, I get to convert an OUString into
UTF-8 character encoding. I'ld like to submit a snippet to explain what
makes me understand.

Hope that helps,
Cedric
<?xml version="1.0"?>
<!--
$RCSfile: $
last change: $Revision: $ $Author: $ $Date: $

(c)2003 by the copyright holders listed with the author-tags.
If no explicit copyright holder is mentioned with a certain author,
the author him-/herself is the copyright holder. All rights reserved.

Public Documentation License Notice:

The contents of this Documentation are subject to the
Public Documentation License Version 1.0 (the "License");
you may only use this Documentation if you comply with
the terms of this License. A copy of the License is
available at http://www.openoffice.org/licenses/PDL.html

The Original Documentation can be found in the CVS archives
of openoffice.org at the place specified by RCSfile: in this header.

The Initial Writer(s) of the Original Documentation are listed
with the author-tags below.

The Contributor(s) are listed with the author-tags below
without the marker for being an initial author.

All Rights Reserved.
-->

<snippet language="Cpp" application="Office">

<keywords>
	<keyword>OUString</keyword>
	<keyword>UTF8</keyword>
	<keyword>Character encoding</keyword>
	<keyword>Conversion</keyword>
</keywords>

<authors>
	<author id="cedricbosdo" initial="true" email="[EMAIL PROTECTED]">C&#233;dric BOSDONNAT</author>
</authors>

<question heading="OUString and UTF8">Howto convert an rtl::OUString into UTF-8 encoding?
<p>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</p>
</question>

<answer>
<p>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.</p>
<p>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 ;)</p>
<listing>/*
   Creation of a test OUString with extended ascii letters

   createFromAscii converts only ascii characters between 0 and 127:
   that&apos;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&apos;s encoding. All the encoding flags
   can be found in rtl/textenc.h
*/
OUString sWrongString = OUSTring::createFromAscii(&quot;tr&#232;s&quot;);
OUString sGoodString = OUString(&quot;tr&#232;s&quot;, 4, RTL_TEXTENCODING_ISO_8859_15);

/*
    Convertion of the OUString into an OString encoded in UTF-8
 */
OString sUtf8String = OUStringToOString(sGoodString, RTL_TEXTENCODING_UTF8);
// The result is &quot;tr&#195;šs&quot;, ie: &quot;tr&#232;s&quot; in UTF8.</listing>
</answer>

<versions>
	<version number="2.0.x" status="tested"/>
</versions>

<operating-systems>
<operating-system name="All"/>
</operating-systems>

<changelog>
</changelog>

</snippet>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to