NSString 转换成NSData 对象
1 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
NSData 转换成NSString对象
1 NSData * data; 2 NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData 转换成char*
1 NSData *data;
2 char *test=[data bytes];
char* 转换成NSData对象
1 byte* tempData = malloc(sizeof(byte)*16);
2 NSData *content = [NSData dataWithBytes:tempData length:16];
1. NSString转化为UNICODE String:
1 (NSString*)fname = @“Test”;
2 char fnameStr[10];
3 memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
与strcpy相比,memcpy并不是遇到' '就结束,而是一定会拷贝完n个字节
2. NSString 转化为 char *
1 NSString * str= @“Test”;
2 const char * a = [str UTF8String];
3.char * 转化为 NSString
1 NSString *str = [NSString stringWithCString encoding:NSUTF8StringEncoding];
4. char * 转化 NSData
方法一:
1 char * a = (char*)malloc(sizeof(byte)*16);
2 NSData *data = [NSData dataWithBytes: a length:strlen(a)];
方法二:
转换为NSString: - (id)initWithUTF8String:(const char *)bytes
然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
5. NSData 转化 char *
1 NSData data ;
2 char* a=[data bytes];
6. NSData 转化 NSString;
1 NSData* data;
2 NSString* aStr = [NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
7. NSString 转化 NSData对象
1 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
NSString 转化 NSURL
1 //NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]]; 2 NSString *urlString=[@"http://www google.com/search?client=safari&rls=en&q=搜索&ie=UTF-8&oe=UTF-8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL 转化 NSString
1 NSURL *url=[NSURL URLWithString:urlString]; 2 NSString *s=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 3 NSArray *arr = [urlStr componentsSeparatedByString:@"&"]; 4 NSURL *url = [NSURL URLWithString:[[arr objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ]]; 5 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 6 [request setHTTPMethod:@"POST"]; 7 8 NSMutableArray *_array = [[NSMutableArray alloc] initWithCapacity:0]; 9 for (int i = 1; i < [arr count]; ++i ){ 10 NSString *str = [arr objectAtIndex:i]; 11 [_array addObject:str]; 12 } 13 [_array componentsJoinedByString:@"&"]; 14 NSData *data = [[_array componentsJoinedByString:@"&"] dataUsingEncoding: NSASCIIStringEncoding]; 15 [request setHTTPBody:data]; 16 // 发送请求并获得服务器反馈的数据 17 NSData *urldata = [AESEnCDeCViewController Get:url]; 18 19 // 第一种 转换NSData数据到char*字符串 20 char * test = (char*)[urldata bytes]; 21 std::string old = deaes(test); 22 23 // 第二种 转换NSData到UTF8编码的NSString中再转换为char*字符串 24 // NSString *desStr = [[NSString alloc] initWithData:urldata encoding:NSUTF8StringEncoding]; 25 // const char *desresult = [desStr UTF8String]; 26 // std::string old = deaes(desresult); 27 28 // 解密字字符串到明文 29 NSString *oldstr = [[NSString alloc] initWithCString:old.c_str()]; 30
很多时候软件读取的中文网页编码集是gb2312,所以显示出来的是乱码。这时需要将NSString文字编码转换
1 NSURL *url = [NSURL URLWithString:urlStr];
2 NSData *data = [NSData dataWithContentsOfURL:url];
3 NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
4 NSString *retStr = [[NSString alloc] initWithData:data encoding:enc];