#import <Foundation/Foundation.h>
@interface NSString (Base64)/** * 转换为Base64编码 */
- (NSString *)base64EncodedString; /** * 将Base64编码还原 */
- (NSString *)base64DecodedString;
@end
@implementation NSString (JCBase64)
- (NSString *)base64EncodedString;
{
NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
return [data base64EncodedStringWithOptions:0];
}
- (NSString *)base64DecodedString
{
NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}
@end
调用:
NSString *codeString = @"Hello world";
NSLog(@"原文--%@",codeString);NSString *base64Str = [codeString base64EncodedString];
NSLog(@"Base64编码--%@",base64Str);NSString *decodeStr = [base64Str base64DecodedString];
NSLog(@"Base64解码--%@",decodeStr);