HBufC16* CPlainTextEditorContainer::ToUnicodeConvertL(TText8* aOrigin)
{
CCnvCharacterSetConverter* converter=CCnvCharacterSetConverter::NewLC();
//RFs fSession;
//需要打开文件服务器
//User::LeaveIfError(fSession.Connect());
//判断传入的源字符串是否GBK\GB2123码
if(converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierGbk,CCoeEnv::Static()->FsSession())!=CCnvCharacterSetConverter::EAvailable)
{
//如果不是GBK\GBK2123码就退出
//fSessoin.Close();
CleanupStack::Pop(converter);
delete converter;
User::Leave(KErrNotSupported);
}
TText8* str = aOrigin;
TInt state=CCnvCharacterSetConverter::KStateDefault;
TPtrC8 source( str );
HBufC* iInfoText = HBufC::NewL( source.Length() );
TPtr16 ptr = iInfoText->Des();
if(CCnvCharacterSetConverter::EErrorIllFormedInput == converter->ConvertToUnicode(ptr, source, state))
User::Leave(KErrArgument);
//转换完成并清除转换器
CleanupStack::PopAndDestroy();
return iInfoText;
}
使用方法如下:
_LIT(path,"C:\\ebooks.txt");
TBuf8<299> rText;
RFileReadTextFileSeekL(path,rText,op_num);
TText8 string[300];
Mem::Copy(string,rText.Ptr(),rText.Size());
string[rText.Size()]='\0';
//RFileWriteTextFileL(, TDes8& aBuffer)
HBufC16* msg = ToUnicodeConvertL(string);
// CAknInformationNote* InfoNote;
//InfoNote = new ( ELeave ) CAknInformationNote;
//InfoNote->ExecuteLD(*msg);
SetEditorText(*msg);
iEditor->DrawDeferred();
delete msg;