在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1. NSString转化为UNICODE String:
(NSString*)fname = @“Test”;
char fnameStr[10];
memcpy(fnameStr, [fname cStringUsingEncoding:NSUnicodeStringEncoding], 2*([fname length]));
与strcpy相比,memcpy并不是遇到'\0'就结束,而是一定会拷贝完n个字节
2. NSString 转化为 char *
NSString * str= @“Test”;
const char * a =[str UTF8String];
3.char * 转化为 NSString
NSString *str=[NSString stringWithCString encoding:NSUTF8StringEncoding];
4. char * 转化 NSData
方法一:
char * a = (char*)malloc(sizeof(byte)*16);
NSData *data = [NSData dataWithBytes: a length:strlen(a)];
方法二:
转换为NSString: - (id)initWithUTF8String:(const char *)bytes
然后用NSString的 - (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
5. NSData 转化 char *
NSData data ;
char* a=[data bytes];
6. NSData 转化 NSString;
NSData* data;
NSString* aStr= [NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
7. NSString 转化 NSData对象 NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding]; 11.NSString 转化 NSURL
NSString *urlString=[@"http://www.google.com/search?client=safari&rls=en&q=搜索&ie=UTF-8&oe=UTF-8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlString]; 12.NSURL 转化 NSString
NSString *s=[[url absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSArray *arr = [urlString componentsSeparatedByString:@"&"]; // 发送请求并获得服务器反馈的数据 // 解密字字符串到明文 很多时候软件读取的中文网页编码集是gb2312,所以显示出来的是乱码。这时需要将NSString文字编码转换
1 NSURL *url = [NSURL URLWithString:urlStr]; |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论