TImCodecB64类是symbian平台下提供的Base64编码解码类,你在官方文档的sdk可能找不到关于Base64编码解码类的描述。但是此类确实存在于sdk的开发包中,在这里给出TImCodecB64类的简单用法示范。
示例代码
#include "imcvcodc.h" //base64 声明头文件
static HBufC8* Base64EncodeLC(const TDesC8 & aSourceBuf)
{
TImCodecB64 B64;
//Using base64 the size is increased by 1/3
HBufC8 * buffer = HBufC8::NewL(aSourceBuf.Length() + aSourceBuf.Length()/3);
B64.Initialise();
TPtr8 buffPtr = buffer->Des();
B64.Encode(aSourceBuf, buffPtr);
return buffer;
}
static HBufC8* Base64DecodeLC(const TDesC8 & aSourceBuf)
{
TImCodecB64 B64;
HBufC8 * buffer = HBufC8::NewLC(aSourceBuf.Length());
B64.Initialise();
TPtr8 buffPtr = buffer->Des();
B64.Decode(aSourceBuf, buffPtr);
return buffer;
}
BASE64编码
//encode string to base64 encoding with clean stack
HBufC8* encodeBuf = StringUtil::Base64EncodeLC(_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
HBufC8* decodeBuf = StringUtil::Base64DecodeLC(_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