demo下载地址:http://pan.baidu.com/s/1i3LvaDZ
在iOS7 中系统提供了base64 的转码方式
示比例如以下:
NSData* originData = [originStr dataUsingEncoding:NSASCIIStringEncoding];
NSString* encodeResult = [originData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSLog(@"encodeResult:%@",encodeResult);
NSData* decodeData = [[NSData alloc] initWithBase64EncodedString:encodeResult options:0];
NSString* decodeStr = [[NSString alloc] initWithData:decodeData encoding:NSASCIIStringEncoding];
iOS 使用开源库 GTMBase64 转码
/**
*GTM 转码
*/
-(void)GTMEncodeTest
{
NSString* originStr = @"Man";
NSString* encodeResult = nil;
NSData* originData = [originStr dataUsingEncoding:NSUTF8StringEncoding];
NSData* encodeData = [GTMBase64 encodeData:originData];
encodeResult = [[NSString alloc] initWithData:encodeData encoding:NSUTF8StringEncoding];
NSLog(@"%@",encodeResult);
}
/**
* GTM 解码
*/
-(void)GMTDecodeTest
{
NSString* encodeStr = @"TWFu";
NSString* decodeResult = nil;
NSData* encodeData = [encodeStr dataUsingEncoding:NSUTF8StringEncoding];
NSData* decodeData = [GTMBase64 decodeData:encodeData];
decodeResult = [[NSString alloc] initWithData:decodeData encoding:NSUTF8StringEncoding];
NSLog(@"%@",decodeResult);
}