×
Namespaces

Variants
Actions

如何转换txt到unicode文本

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
翻译:
hoolee
最后由 hamishwillee 在 15 Dec 2011 编辑




CCnvCharacterSetConverter API用来转换text到unicode格式。按下列函数来实现编码的转换:

void ConvertTextL(const TDesC8& aData,TDes& aText, TUint aConverter)
{
TInt state = CCnvCharacterSetConverter::KStateDefault;
CCnvCharacterSetConverter* CSConverter =
CCnvCharacterSetConverter::NewLC();
 
TPtrC8 Remainder(aData);
 
if (CSConverter->PrepareToConvertToOrFromL(aConverter,iFs) !=
CCnvCharacterSetConverter::EAvailable)
{
CSConverter->PrepareToConvertToOrFromL(aConverter,iFs);
}
 
for(;;)
{
const TInt returnValue =
CSConverter->ConvertToUnicode(aText,Remainder,state);
if (returnValue <= 0) // < error
{
break;
}
 
Remainder.Set(Remainder.Right(returnValue));
}
 
CleanupStack::PopAndDestroy(CSConverter);
}

aData用来按字节buffer传输文本,你可以使用file reading example codes来读取文件buffer。aText然后可以用来存储Unicode编码的文本,注意调用者要为aText分配足够多的内存。aConverter用来标记转换表示,表明你询问的需要转换的字符类型:

CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* CharSets = 
CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC(CCoeEnv::Static()->FsSession());
 
for(TInt i=0; i < CharSets->Count(); i++)
{
//CharSets->At(i).NameIsFileName()
//CharSets->At(i).Name()
//CharSets->At(i).Identifier()
}
 
CleanupStack::PopAndDestroy(); // CharSets

可以从 Identifier()函数获得aConverter值,从Name()函数获得converter的名字,这可以是converter文件的全名或者字符集名字(如windows-874),要检查Name()函数返回值,你可以使用NameIsFileName()函数。n.

注意,手机型号不同,支持的字符集名称也不同。如TIS-620,通常命名为TIS_620,这样如果你要检查特定的字符转换,应该手动检查

This page was last modified on 15 December 2011, at 05:22.
117 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×