首页 > Personal > ios > NSString NSData char* 相互转换
2015
01-22

NSString NSData char* 相互转换

NSString 转 UNICODE char*:
NSString* value = @“value”;
cosnt char* str = [value cStringUsingEncoding:NSUnicodeStringEncoding];

NSString 转 utf8 char*
NSString* value= @“value”;
const char* str =[value UTF8String];

utf8 char* 转 NSString
NSString* value = [NSString stringWithCString:str encoding:NSUTF8StringEncoding];

char* 转 NSData
NSData* data = [NSData dataWithBytes:str length:strlen(str)];
NSString* value = [NSString initWithUTF8String:str];
NSData* data = [value dataUsingEncoding:NSUTF8StringEncoding];

NSData 转 char*
char* str = [data bytes];

NSData 转 NSString;
NSString* value = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

NSString 转 NSData
NSData* data = [@”vale” dataUsingEncoding:NSUTF8StringEncoding];

NSString 转 NSURL
NSString* url_value = [value stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:url_value];

NSURL 转 NSString
NSURL* url = [NSURL URLWithString:url_value];
NSString* value = [[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

最后编辑:
作者:wy182000
这个作者貌似有点懒,什么都没有留下。