原理:
1>ASCII碼是8個(gè)二進(jìn)制位一編碼
2>Base64編碼是6個(gè)二進(jìn)制位一編碼,所以轉(zhuǎn)換成字符串后會(huì)比ASCII內(nèi)容要多
調(diào)用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轉(zhuǎn)換成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;
}