//NSData 转 NSString
NSData *data;
NSString*str = [[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSString 转 NSData
NSString *string;
NSData*data = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
//char 转 NSString
char *str1;
NSString*str = [NSString stringWithCString:str1 encoding:NSUTF8StringEncoding];
//NSString 转 char
NSString *str;
char *str1 = [str UTF8String];
//自转换
NSString*string;
string = [string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//也可以修改NSString类
//NSString+URLEncoding.h
#import<Foundation/Foundation.h>
@interface NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString;
- (NSString *)URLDecodedString;
@end
//NSString+URLEncoding.m
#import"NSString+URLEncoding.h"
@implementation NSString (OAURLEncodingAdditions)
- (NSString *)URLEncodedString
{
NSString*result = (NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:@&=+$,/?%#[]"),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
//解决url乱码
- (NSString*)URLDecodedString
{
NSString*result = (NSString*)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(kCFAllocatorDefault,
(CFStringRef)self,
CFSTR(""),
kCFStringEncodingUTF8);
[result autorelease];
return result;
}
@end