『导言』
iOS 开发中如何使NSData和NSSting相互转化?
str转data:
initWithData: encoding:
data转str:dataUsingEncoding:
-
NSString ->NSData
- 方法:
dataUsingEncoding:
NSString *str = @"username=zwj&pwd=123&type=JSON";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"\nstr = %@\n-------\n data = %@\n",str,data);
- 打印结果str->data
str = username=zwj&pwd=123&type=JSON
data = <75736572 6e616d65 3d7a776a 26707764 3d313233 26747970 653d4a53 4f4e>
-
NSData -> NSString
- 方法:
initWithData: encoding:
NSString *str2 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"\n data = %@\n ---------\n ,str2 = %@ \n ",data,str2 );
- 打印结果 data->str
data = <75736572 6e616d65 3d7a776a 26707764 3d313233 26747970 653d4a53 4f4e>
,str2 = username=zwj&pwd=123&type=JSON
网友评论