Symbian3/PDK/Source/GUID-86086CA3-9A2E-4024-BE80-1763614A5079.dita
changeset 1 25a17d01db0c
child 3 46218c8b8afa
equal deleted inserted replaced
0:89d6a7a84779 1:25a17d01db0c
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-86086CA3-9A2E-4024-BE80-1763614A5079" xml:lang="en"><title>Character
       
    13 Conversion in Symbian C++ and GNU C</title><shortdesc>Symbian C++ supports character conversion between Unicode and other
       
    14 native character sets.</shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    15 <p>GNU C supports character conversion: <ul>
       
    16 <li><p>between Unicode and other native character sets</p></li>
       
    17 <li><p>from a native character set to another native character set.</p></li>
       
    18 </ul></p>
       
    19 <p>The following example illustrates the steps to be followed to convert from
       
    20 one native character set to another native character set. In GNU C it is straightforward.
       
    21 In Symbian C++ more lines of code are needed to do the same work. The example
       
    22 converts a string from the ISO-8859-1 character set to the ISO-8859-2
       
    23 character set. </p>
       
    24 <p><b>GNU C</b></p>
       
    25 <codeblock xml:space="preserve">#include &lt;iconv.h&gt;
       
    26 #include &lt;stddef.h&gt;
       
    27 #include &lt;stdio.h&gt;
       
    28 #include &lt;string.h&gt;
       
    29 #include &lt;errno.h&gt;
       
    30 
       
    31 int main (void)
       
    32 {
       
    33  iconv_t cd;
       
    34  const char *inbuf = "abcd";
       
    35  char *outbuf = NULL;;
       
    36  size_t inbytes,outbytes;
       
    37  cd = iconv_open ("ISO-8859-2", "ISO-8859-1");
       
    38  if (cd == (iconv_t) -1)
       
    39  {
       
    40    printf ("iconv_open failed errno = %d\n",errno);
       
    41    iconv_close(cd);
       
    42    return 1;
       
    43  }
       
    44  outbuf = (char*) malloc(4);
       
    45  inbytes = outbytes = 4 ;
       
    46  iconv (cd, &amp;inbuf, &amp;inbytes, &amp;outbuf, &amp;outbytes);
       
    47  if (iconv (cd, &amp;inbuf, &amp;inbytes, &amp;outbuf, &amp;outbytes) == (size_t) -1)
       
    48      printf(" errno = %d \n",errno);
       
    49  else
       
    50        printf("Iconv passed\n");
       
    51  iconv_close (cd);
       
    52  return 1;
       
    53 }
       
    54 </codeblock>
       
    55 <p><b>SYMBIAN C++ </b></p>
       
    56 <ul>
       
    57 <li><p>Specify the non-Unicode character set being converted to Unicode. In
       
    58 this case it is ISO-8859-1.</p></li>
       
    59 <li><p>Convert the text from the native character set to Unicode.</p></li>
       
    60 <li><p>Specify the non-Unicode character set being converted from Unicode.
       
    61 In this case it is ISO-8859-2 </p></li>
       
    62 <li><p>Convert the text from Unicode to the native character set </p></li>
       
    63 </ul>
       
    64 <codeblock xml:space="preserve">#include &lt;e32base.h&gt;
       
    65 #include &lt;e32cons.h&gt;
       
    66 #include &lt;charconv.h&gt;
       
    67 #include &lt;f32file.h&gt;
       
    68 #include &lt;string.h&gt;
       
    69 #include &lt;stdlib.h&gt;
       
    70 
       
    71 LIT8(KNativeText, "abcd");
       
    72 LOCAL_C int doExampleL()
       
    73 {
       
    74    char *inbuf = "abcd";
       
    75    char *outbuf = NULL;
       
    76    int inbytes,outbytes;
       
    77    inbytes = outbytes = 4; 
       
    78    
       
    79    TInt retVal = KErrNone;
       
    80    outbuf = (char*) malloc(outbytes);
       
    81    RFs fileSession;
       
    82    User::LeaveIfError(fileSession.Connect());
       
    83    CleanupClosePushL(fileSession);
       
    84   
       
    85   //Allocates and constructs a CCnvCharacterSetConverter object
       
    86   CnvCharacterSetConverter* conv = CCnvCharacterSetConverter::NewLC() ;
       
    87   TPtrC8 remainderOfForeignText((const TText8*) inbuf,inbytes);
       
    88   TBuf16&lt;256&gt; UnicodeText;
       
    89   TBuf8&lt;256&gt; outputBuffer;
       
    90   TInt numberOfUnconvertibleCharacters = 0;
       
    91   TInt indexOfFirstByteOfFirstUnconvertibleCharacter = 0;
       
    92   //Specifies the character set(ISO-8859-1) to convert to UNICODE
       
    93   CCnvCharacterSetConverter::TAvailability  avail = conv-&gt;PrepareToConvertToOrFromL (KCharacterSetIdentifierIso88591, fileSession);
       
    94  if(CCnvCharacterSetConverter::ENotAvailable == avail)
       
    95  {
       
    96    CleanupStack::PopAndDestroy(2);  //conv, fileSession
       
    97    return KErrGeneral;
       
    98  }
       
    99  //Convert text encoded in the ISO-8859-1 character set into the Unicode character set (UCS-2).
       
   100  retVal = conv-&gt;ConvertToUnicode(UnicodeText, remainderOfForeignText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter);
       
   101  if(retVal &lt; 0 &amp;&amp; (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
       
   102  {
       
   103   CleanupStack::PopAndDestroy(2);  //conv, fileSession
       
   104   return retVal;
       
   105  }
       
   106  //Specifies the character set(ISO-8859-2) to convert from UNICODE
       
   107  avail = conv-&gt;PrepareToConvertToOrFromL(KCharacterSetIdentifierIso88592, fileSession);
       
   108  if(CCnvCharacterSetConverter::ENotAvailable == avail)
       
   109  {
       
   110    CleanupStack::PopAndDestroy(2);  //conv, fileSession
       
   111    return KErrGeneral;
       
   112  }
       
   113  //Convert text encoded in the Unicode character set (UCS-2) to  ISO-8859-2
       
   114  retVal = conv-&gt;ConvertFromUnicode(outputBuffer,UnicodeText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter);
       
   115  if(retVal &lt; 0 &amp;&amp; (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
       
   116  {
       
   117    CleanupStack::PopAndDestroy(2);  //conv, fileSession
       
   118    return retVal;
       
   119  }
       
   120  TInt outputbufferLength = outputBuffer.Length();
       
   121  strncpy(outbuf, (const char*) outputBuffer.Ptr(),outputbufferLength);
       
   122  outbuf = outbuf + outputbufferLength;
       
   123  CleanupStack::PopAndDestroy(2); //conv, fileSession
       
   124  return retVal;
       
   125 }
       
   126 </codeblock>
       
   127 </conbody></concept>