1.NSString 转化为 char*
NSString *testString = @"this is a test";
const char * testChar = [testString UTF8String];
2.char * 转化为 NSString
NSString *str = [NSString stringWithCString:testChar encoding:NSUTF8StringEncoding];
3.char * 转化 NSData
方法一:
char* testChar = (char*)malloc(sizeof(byte)*16);
NSData *dataTest = [NSData dataWithBytes:testChar length:strlen(testChar)];
方法二:
先转化为NSString: NSString *testString = [[NSString alloc] initWithUTF8String:testChar];
后NSString转: NSData *dataTest = [testString dataUsingEncoding: NSUTF8StringEncoding];
5.NSData 转化 char *
NSData dataTest;
char* testChar = [dataTest bytes];
6.NSData 转化 NSString;
NSData* dataTest;
NSString* testString= [[NSString alloc] initWithData:dataTest encoding:NSUTF8StringEncoding];
7.NSString 转化 NSData对象
NSData* testData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
8.NSArray与NSData
NSArray *arr1 = [[NSArray alloc]initWithObjects:@"HMT",@"WDQ",nil];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:arr1];
NSArray *arr2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"arr2 = %@",arr2);
9.image与NSData
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}