NSString和NSData,Base64的转换

1.NSString明文 转Base64格式

NSString ==> NSData ==> Base64

NSString *string = @"要加密的内容";
//NSString转换成NSData
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// base64格式的字符串
NSString * stringBase64 = [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
//NSString *stringBase64 = [data base64Encoding]; (不建议使用)

2.Base64格式 转NSString

Base64 ==> NSData ==> NSString

NSData *data = [[NSData alloc] initWithBase64EncodedString:stringBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *string =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

3.NSData 转成 Char*

NSData *data; 
char *testChar = [data bytes];

4.Char* 转成 NSData

byte* tempData = malloc(sizeof(byte)*16); 
NSData *content=[NSData dataWithBytes:tempData length:16];

你可能感兴趣的:(NSString和NSData,Base64的转换)