Symbian 中字符串 转码 base64
2010-05-06 17:24:00 来源:WEB开发网需要头文件及库:
imcvcodc.h imut.lib
用到的类:
TImCodecB64
编码:
HBufC8* CBase64Handler::Base64EncodeLC( const TDesC8 & aSourceBuf )
{
TImCodecB64 B64;
buffer8 = HBufC8::NewL( aSourceBuf.Length() + aSourceBuf.Length()/3 );
B64.Initialise();
TPtr8 buffPtr = buffer8->Des();
B64.Encode( aSourceBuf, buffPtr );
return buffer8;
}
HBufC8* CBase64Handler::Base64EncodeLC( const TDesC16 & aSourceBuf )
{
buffer16 = HBufC8::NewLC( aSourceBuf.Length() );
buffer16->Des().Copy( aSourceBuf );
return Base64EncodeLC( *buffer16 );
}
结果显示:
dGhpcyBpcyBOZXNOOQ==
解码:
HBufC8* CBase64Handler::Base64DecodeLC( const TDesC8 & aSourceBuf )
{
TImCodecB64 B64;
buffer = HBufC8::NewLC( aSourceBuf.Length() );
B64.Initialise();
TPtr8 buffPtr = buffer->Des();
B64.Decode( aSourceBuf, buffPtr );
return buffer;
}
结果显示:
this is test!
测试方法:
CBase64Handler* Handler = CBase64Handler::NewL();
HBufC8* encodeBuf = Handler->Base64EncodeLC(_L("this is test!"));
HBufC *enbuff16 = HBufC::NewLC( (*encodeBuf).Length() );
enbuff16->Des().Copy( *encodeBuf );
console->Write( enbuff16->Des() );
console->Write( _L(" ") );
HBufC8* decodeBuf = Handler->Base64DecodeLC( ( *encodeBuf ) );
HBufC *debuff16 = HBufC::NewLC( (*decodeBuf).Length() );
debuff16->Des().Copy( *decodeBuf );
console->Write( debuff16->Des() );
console->Write( _L(" ") );
CleanupStack::PopAndDestroy(4);
更多精彩
赞助商链接