Summary of interchange of utf8 and Unicode in Symbian
1. Read utf8 text and convert it to uncoide text
(Here there is a problem that can be called an error, that is, the principle of file operations is to use what to read and what to write,
Therefore, the file. Read (TP) and outputfilestream. writel (* Unicode) results are correct but at risk)
Void utf82unicode ()
{
Rfs fs;
FS. Connect ();
Rfile file;
File. Open (FS, _ L ("C: // data // utf8.txt"), efileread );
Hbufc8 * utf8 = NULL;
Tint size = 0;
File. Size (size );
Utf8 = hbufc8: newlc (size );
Tptr8 TP = utf8-> des ();
File. Read (TP );
File. Close ();
Hbufc * Unicode = cnvutfconverter: converttounicodefromutf8l (TP );
File. Replace (FS, _ L ("C: // data // unicode.txt"), efilewrite );
Cleanupclosepushl (File );
Rfilewritestream outputfilestream (File );
Cleanupclosepushl (outputfilestream );
Outputfilestream. writel (* Unicode );
Cleanupstack: popanddestroy (2); // outputfilestream, file
FS. Close ();
Cleanupstack: popanddestroy ();
Delete Unicode;
}
2. The second method for uncoide text storage after reading utf8 text and converting it
Tint unicodelittlefromunicodebig (const tdesc & abigdes, tdes8 & alittledes)
{
Rfs fs;
FS. Connect ();
Ccnvcharactersetconverter * iconv = NULL;
Iconv = ccnvcharactersetconverter: newlc ();
If (null! = Iconv)
{
If (iconv-> preparetoconverttoorfroml (kcharactersetidentifierunicodelittle,
FS )! = Ccnvcharactersetconverter: eavailable)
{
FS. Close ();
User: Leave (kerrnotsupported );
}
Iconv-> convertfromunicode (alittledes, abigdes );
Cleanupstack: popanddestroy ();
}
Else
{
FS. Close ();
Return kerrnotsupported;
}
FS. Close ();
Return kerrnone;
}
Void utf82unicode ()
{
Rfs fs;
FS. Connect ();
Rfile file;
File. Open (FS, _ L ("C: // data // utf8.txt"), efileread );
Hbufc8 * utf8 = NULL;
Tint size = 0;
File. Size (size );
Utf8 = hbufc8: newlc (size );
Tptr8 TP = utf8-> des ();
File. Read (TP );
File. Close ();
Hbufc * Unicode = cnvutfconverter: converttounicodefromutf8l (TP );
Cleanupstack: popanddestroy (); // utf8
Hbufc8 * unicode8 = hbufc8: newl (Unicode-> length () * 2 );
Tptr8 tpunicode8 = unicode8-> des ();
Unicodelittlefromunicodebig (* Unicode, tpunicode8 );
Delete Unicode;
File. Replace (FS, _ L ("C: // data // utf82unicode.txt"), efilewrite );
Cleanupclosepushl (File );
File. Write (* unicode8 );
Cleanupstack: popanddestroy (); // File
FS. Close ();
Delete unicode8;
}
3. Read uncoide text converted and stored as utf8 text
Void unicodelittletounicodebig (TDES & abigdes, const tdesc8 & alittledes)
{
Rfs fs;
FS. Connect ();
Ccnvcharactersetconverter * converter = ccnvcharactersetconverter: newlc ();
If (converter-> preparetoconverttoorfroml (kcharactersetidentifierunicodelittle,
FS) = ccnvcharactersetconverter: eavailable)
{
// Do nothing about it
}
Tint state = ccnvcharactersetconverter: kstatedefault;
If (ccnvcharactersetconverter: eerrorillformedinput
= Converter-> converttounicode (abigdes, alittledes, State ))
{
FS. Close ();
Cleanupstack: popanddestroy ();
User: Leave (kerrargument); // leave if error in conversion.
}
Cleanupstack: popanddestroy (); // clean for Converter
FS. Close ();
}
Void unicode2utf8 ()
{
Rfs fs;
FS. Connect ();
Rfile file;
File. Open (FS, _ L ("C: // data // unicode.txt"), efileread );
Tint size = 0;
File. Size (size );
Hbufc8 * unicode8 = hbufc8: newl (size );
Tptr8 tpu8 = unicode8-> des ();
File. Read (tpu8 );
Hbufc * unicode16 = hbufc: newl (size );
// Unicode16-> des (). Copy (tpu8 );
Tptr tpu16 = unicode16-> des ();
Unicodelittletounicodebig (tpu16, * unicode8 );
Delete unicode8;
File. Close ();
Hbufc8 * utf8 = hbufc8: newl (size );
Tptr8 tputf8 = utf8-> des ();
Cnvutfconverter: convertfromunicodetoutf8 (tputf8, * unicode16 );
File. Replace (FS, _ L ("C: // data // unicode2utf8.txt"), efilewrite );
File. Write (tputf8 );
Delete unicode16;
FS. Close ();
}
4. The second method for reading uncoide text after conversion is stored as utf8 text. Refer to 1. Use rfilereadstream to inherit and use readl (tdes16 & ades) in the base class rreadstream to read files.