Character Conversion in Symbian C++ and GNU C

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;
}