Symbian中的一些编码转换API

前言:在Symbian中的中文字都是用unicode编码来完成的 ,所以通过功能函数转换为uncode或者保存为unicode文件就可以解决中文显示的问题了。

 

以下是Symbian中提供的几个关于编码格式转换的API接口和粗略说明 ( 以功能逐渐强大为序 ):

 

1) Class EscapeUtils

Description

Provides an API to allow data to be escape encoded and decoded. Also provide an API for converting a UNICODE data (16-bit descriptor) into UTF8 data (8-bit descriptor) and vice-verse.

注:EscapeUtils工具类中也包含了将URL进行编码的简单接口(不好用)。

 

 Location: EscapeUtils.h
Link against: inetprotutil.lib

 

 

2) class CnvUtfConverter

Description

Converts text between Unicode (UCS-2) and the two Unicode transformation formats UTF-7 and UTF-8. There are no functions to convert directly between UTF-7 and UTF-8.

 

Objects of this class do not need to be created because all the member functions are static. The four functions are passed text in the second argument and output the resulting text in the first argument. Sixteen-bit descriptors are used to hold text encoded in UCS-2 (i.e. normal 16 bit Unicode), and eight-bit descriptors are used to hold text encoded in either of the transformation formats.

 

The conversion functions return the number of characters which were not converted because the output descriptor was not long enough to hold all of the converted text. This allows users of this class to perform partial conversions on an input descriptor, handling the case when the input descriptor is truncated mid way through a multi-byte character. The caller does not have to guess how big to make the output descriptor for a given input descriptor- they can simply do the conversion in a loop using a small output descriptor. The ability to handle truncated descriptors is particularly useful if the caller is receiving information in chunks from an external source.

 

Location: UTF.H
Link against: charconv.lib

 

 

3) Class CCnvCharacterSetConverter

 

示例代码:

  
    
TText * testtext = (TText * ) " 测试文字,test " ;
TPtr ptr
= GBKToUnicodeL( testtext ); // then, the ptr can be display on screen.

// ---------------------------------------

HBufC16
* GBKToUnicodeL(TText * aText)
{
// CcnvCharacterSetConverter API来转换
CCnvCharacterSetConverter * converter = CCnvCharacterSetConverter::NewLC();

// 判断当前系统支持的字符集里面是否包括GBK.
// 方法1.START.
if ( converter -> PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk, myRFs)
!= CCnvCharacterSetConverter::EAvailable )
User::Leave(KErrNotSupported);
// 方法1.END.

// 方法2.START.
/*
CArrayFixFlat<SCharacterSet>* chSetArray = NULL;
chSetArray = CreateArrayOfCharacterSetsAvailableLC(myRFs);
... // 在chSetArray中寻找当前系统支持的字符集里面是否包括GBK
CleanupStack::PopAndDestroy(chSetArray);
//
*/
// 方法2.END.

TText8
* str = (TText8 * )aText;
TInt state
= CCnvCharacterSetConverter::KStateDefault;
TPtrC8 source( str );
HBufC
* infoText = HBufC::NewL( source.Length() * 2 );
TPtr ptr
= infoText -> Des();
if ( converter -> ConvertToUnicode(ptr, source, state)
== CCnvCharacterSetConverter::EErrorIllFormedInput )
User::Leave(KErrArgument);
// Leave if error in conversion.

CleanupStack::PopAndDestroy(converter);
// clean for converter

return infoText; // 经过转换,infoText中的中文字符就可以在设备上显示了。
}

 

你可能感兴趣的:(Symbian)