S60 Open C
Open C Localization

Open C Localization

Table of Contents

Character conversion in Symbian C++ and GNU C compared

 


Character conversion in Symbian C++ and GNU C compared

Symbian C++ supports character conversion between Unicode and other native character sets.

GNU C supports character conversion:

  • between Unicode and other native character sets
  • from a native character set to another native character set.

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.

GNU C

#include <iconv.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

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, &inbuf, &inbytes, &outbuf, &outbytes);
 if (iconv (cd, &inbuf, &inbytes, &outbuf, &outbytes) == (size_t) -1)
     printf(" errno = %d \n",errno);
 else
       printf("Iconv passed\n");
 iconv_close (cd);
 return 1;
}

SYMBIAN C++

  • Specify the non-Unicode character set being converted to Unicode. In this case it is ISO-8859-1.
  • Convert the text from the native character set to Unicode.
  • Specify the non-Unicode character set being converted from Unicode. In this case it is ISO-8859-2
  • Convert the text from Unicode to the native character set
#include <e32base.h>
#include <e32cons.h>
#include <charconv.h>
#include <f32file.h>
#include <string.h>
#include <stdlib.h>
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<256> UnicodeText;
  TBuf8<256> outputBuffer;
  TInt numberOfUnconvertibleCharacters = 0;
  TInt indexOfFirstByteOfFirstUnconvertibleCharacter = 0;
  //Specifies the character set(ISO-8859-1) to convert to UNICODE
  CCnvCharacterSetConverter::TAvailability  avail = conv->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->ConvertToUnicode(UnicodeText, remainderOfForeignText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter);
 if(retVal < 0 && (retVal != CCnvCharacterSetConverter::EErrorIllFormedInput))
 {
  CleanupStack::PopAndDestroy(2);  //conv, fileSession
  return retVal;
 }
 //Specifies the character set(ISO-8859-2) to convert from UNICODE
 avail = conv->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->ConvertFromUnicode(outputBuffer,UnicodeText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter);
 if(retVal < 0 && (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;
}

Currently Symbian C++ supports around 50 character sets, while GNU C supports more number of character sets. A list of the character sets supported in Symbian C++ can be found in the charconv.h header file.

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.