Symbian3/SDK/Source/GUID-86086CA3-9A2E-4024-BE80-1763614A5079.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Tue, 20 Jul 2010 12:00:49 +0100
changeset 13 48780e181b38
parent 0 89d6a7a84779
permissions -rw-r--r--
Week 28 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 1897 and Bug 1522.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE concept
  PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
<concept id="GUID-86086CA3-9A2E-4024-BE80-1763614A5079" xml:lang="en"><title>Character
Conversion in Symbian C++ and GNU C</title><shortdesc>Symbian C++ supports character conversion between Unicode and other
native character sets.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
<p>GNU C supports character conversion: <ul>
<li><p>between Unicode and other native character sets</p></li>
<li><p>from a native character set to another native character set.</p></li>
</ul></p>
<p>The following example illustrates the steps to be followed to convert from
one native character set to another native character set. In GNU C it is straightforward.
In Symbian C++ more lines of code are needed to do the same work. The example
converts a string from the ISO-8859-1 character set to the ISO-8859-2
character set. </p>
<p><b>GNU C</b></p>
<codeblock xml:space="preserve">#include &lt;iconv.h&gt;
#include &lt;stddef.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;errno.h&gt;

int main (void)
{
 iconv_t cd;
 const char *inbuf = "abcd";
 char *outbuf = NULL;;
 size_t inbytes,outbytes;
 cd = iconv_open ("ISO-8859-2", "ISO-8859-1");
 if (cd == (iconv_t) -1)
 {
   printf ("iconv_open failed errno = %d\n",errno);
   iconv_close(cd);
   return 1;
 }
 outbuf = (char*) malloc(4);
 inbytes = outbytes = 4 ;
 iconv (cd, &amp;inbuf, &amp;inbytes, &amp;outbuf, &amp;outbytes);
 if (iconv (cd, &amp;inbuf, &amp;inbytes, &amp;outbuf, &amp;outbytes) == (size_t) -1)
     printf(" errno = %d \n",errno);
 else
       printf("Iconv passed\n");
 iconv_close (cd);
 return 1;
}
</codeblock>
<p><b>SYMBIAN C++ </b></p>
<ul>
<li><p>Specify the non-Unicode character set being converted to Unicode. In
this case it is ISO-8859-1.</p></li>
<li><p>Convert the text from the native character set to Unicode.</p></li>
<li><p>Specify the non-Unicode character set being converted from Unicode.
In this case it is ISO-8859-2 </p></li>
<li><p>Convert the text from Unicode to the native character set </p></li>
</ul>
<codeblock xml:space="preserve">#include &lt;e32base.h&gt;
#include &lt;e32cons.h&gt;
#include &lt;charconv.h&gt;
#include &lt;f32file.h&gt;
#include &lt;string.h&gt;
#include &lt;stdlib.h&gt;

LIT8(KNativeText, "abcd");
LOCAL_C int doExampleL()
{
   char *inbuf = "abcd";
   char *outbuf = NULL;
   int inbytes,outbytes;
   inbytes = outbytes = 4; 
   
   TInt retVal = KErrNone;
   outbuf = (char*) malloc(outbytes);
   RFs fileSession;
   User::LeaveIfError(fileSession.Connect());
   CleanupClosePushL(fileSession);
  
  //Allocates and constructs a CCnvCharacterSetConverter object
  CnvCharacterSetConverter* conv = CCnvCharacterSetConverter::NewLC() ;
  TPtrC8 remainderOfForeignText((const TText8*) inbuf,inbytes);
  TBuf16&lt;256&gt; UnicodeText;
  TBuf8&lt;256&gt; outputBuffer;
  TInt numberOfUnconvertibleCharacters = 0;
  TInt indexOfFirstByteOfFirstUnconvertibleCharacter = 0;
  //Specifies the character set(ISO-8859-1) to convert to UNICODE
  CCnvCharacterSetConverter::TAvailability  avail = conv-&gt;PrepareToConvertToOrFromL (KCharacterSetIdentifierIso88591, fileSession);
 if(CCnvCharacterSetConverter::ENotAvailable == avail)
 {
   CleanupStack::PopAndDestroy(2);  //conv, fileSession
   return KErrGeneral;
 }
 //Convert text encoded in the ISO-8859-1 character set into the Unicode character set (UCS-2).
 retVal = conv-&gt;ConvertToUnicode(UnicodeText, remainderOfForeignText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter);
 if(retVal &lt; 0 &amp;&amp; (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
 {
  CleanupStack::PopAndDestroy(2);  //conv, fileSession
  return retVal;
 }
 //Specifies the character set(ISO-8859-2) to convert from UNICODE
 avail = conv-&gt;PrepareToConvertToOrFromL(KCharacterSetIdentifierIso88592, fileSession);
 if(CCnvCharacterSetConverter::ENotAvailable == avail)
 {
   CleanupStack::PopAndDestroy(2);  //conv, fileSession
   return KErrGeneral;
 }
 //Convert text encoded in the Unicode character set (UCS-2) to  ISO-8859-2
 retVal = conv-&gt;ConvertFromUnicode(outputBuffer,UnicodeText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter);
 if(retVal &lt; 0 &amp;&amp; (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
 {
   CleanupStack::PopAndDestroy(2);  //conv, fileSession
   return retVal;
 }
 TInt outputbufferLength = outputBuffer.Length();
 strncpy(outbuf, (const char*) outputBuffer.Ptr(),outputbufferLength);
 outbuf = outbuf + outputbufferLength;
 CleanupStack::PopAndDestroy(2); //conv, fileSession
 return retVal;
}
</codeblock>
</conbody></concept>