原理:
1>ASCII码是8个二进制位一编码
2>Base64编码是6个二进制位一编码,所以转换成字符串后会比ASCII内容要多
调用Base64编码与解码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
NSString *man = [self encode:@"Man"];
NSLog(@"%@",man);
NSString *orginMan = [self dencode:man];
NSLog(@"%@",orginMan);
}
//64编码
- (NSString *)encode:(NSString *)string
{
//先将string转换成data
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSData *base64Data = [data base64EncodedDataWithOptions:0];
NSString *baseString = [[NSString alloc]initWithData:base64Data encoding:NSUTF8StringEncoding];
return baseString;
}
//64解码
- (NSString *)dencode:(NSString *)base64String
{
//NSData *base64data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [[NSData alloc]initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
return string;
}