Symbian平台下Base64编码及解码
2010-05-31 19:30:00 来源:WEB开发网Symbian平台下Base64编码及解码
示例代码
#include "imcvcodc.h" //base64 声明头文件
static TPtrC8 Base64EncodeC(const TDesC8 & aSourceBuf)
{
TImCodecB64 B64;
//Using base64 the size is increased by 1/3
HBufC8 * buffer = HBufC8::NewLC(aSourceBuf.Length() + aSourceBuf.Length()/3);
B64.Initialise();
TPtr8 buffPtr = buffer->Des();
B64.Encode(aSourceBuf, buffPtr);
return TPtrC8(buffer->Des());
}
static TPtrC8 Base64DecodeC(const TDesC8 & aSourceBuf)
{
TImCodecB64 B64;
HBufC8 * buffer = HBufC8::NewLC(aSourceBuf.Length());
B64.Initialise();
TPtr8 buffPtr = buffer->Des();
B64.Decode(aSourceBuf, buffPtr);
return TPtrC8(buffer->Des());
}
BASE64编码
//encode string to base64 encoding with clean stack
TPtrC8 ptr = StringUtil::Base64EncodeC(_L8("This is test"));
//Change to TDesC16
HBufC *buff16 = HBufC::NewLC(ptr.Length());
buff16->Des().Copy(ptr);
//Do something....
...
//clean buffer
CleanupStack::PopAndDestroy(2);
结果
VGhpcyBpcyB0ZXN0
BASE64编码
//Decode base64 encoding with clean stack
TPtrC8 ptr = StringUtil::Base64DecodeC(_L8("VGhpcyBpcyB0ZXN0"));
//Change to TDesC16
HBufC *buff16 = HBufC::NewLC(ptr.Length());
buff16->Des().Copy(ptr);
//Do something
...
//clean buffer
CleanupStack::PopAndDestroy(2);
结果
This is test
提示
除了需要包含”imcvcodc.h”头文件外,你还需要在.mmp文件中加入 imut.lib
赞助商链接