Week 23 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.
<?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 <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;
}
</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 <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;
}
</codeblock>
</conbody></concept>