|
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 <iconv.h> |
|
26 #include <stddef.h> |
|
27 #include <stdio.h> |
|
28 #include <string.h> |
|
29 #include <errno.h> |
|
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, &inbuf, &inbytes, &outbuf, &outbytes); |
|
47 if (iconv (cd, &inbuf, &inbytes, &outbuf, &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 <e32base.h> |
|
65 #include <e32cons.h> |
|
66 #include <charconv.h> |
|
67 #include <f32file.h> |
|
68 #include <string.h> |
|
69 #include <stdlib.h> |
|
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<256> UnicodeText; |
|
89 TBuf8<256> 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->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->ConvertToUnicode(UnicodeText, remainderOfForeignText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter); |
|
101 if(retVal < 0 && (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->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->ConvertFromUnicode(outputBuffer,UnicodeText, numberOfUnconvertibleCharacters, indexOfFirstByteOfFirstUnconvertibleCharacter); |
|
115 if(retVal < 0 && (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> |