Base64编码与解码

首先,将附件中的文件解压后放入项目中,然后,新建一个用来编码和解码的类。

 

头文件:

 

#import <Foundation/Foundation.h>

@interface Base64 : NSObject {
	
}

+ (NSString *) encodeBase64:(NSString *) input;
+ (NSString *) decodeBase64:(NSString *) input;

@end

 

实现文件:

 

#import "Base64.h"
#import "GTMBase64.h"

@implementation Base64

+ (NSString *) encodeBase64:(NSString *) input{
	NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
	data = [GTMBase64 encodeData:data];
	NSString *base64String = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	return base64String;
}

+ (NSString *) decodeBase64:(NSString *) input{
	NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
	data = [GTMBase64 decodeData:data];
	NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	return string;
}

@end

 

示例:

 

NSLog(@"%@", [Base64 encodeBase64:@"Hello, World"]);
NSLog(@"%@", [Base64 decodeBase64:@"SGVsbG8sIFdvcmxk"]);

 

你可能感兴趣的:(base64)