代码1:NSString转换成NSData类型
NSString * str = @"it's CoderZb";
NSData * data1 = [str dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"data1 is %@",data1);
代码1输出
data1 is <68656c6c 6f2c2077 6f726c64 21>
代码1:NSData转换成NSString类型
NSString * Newstr = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];
NSLog(@"%@", Newstr);
代码1输出
it's CoderZb
代码2:Byte转换成NSData类型
int i = 123456;
NSData *data2 = [NSData dataWithBytes:&i length:sizeof(i)];
NSLog(@"data2 is %@",data2);
代码2输出
data2 is <40e20100>
代码3:Byte转换成NSData类型
const char * string = "It's Zb";
NSData * data = [NSData dataWithBytes:string length:strlen(string)+1];
NSLog(@"data3 is %@",data);
代码3输出
data3 is <49742773 205a6200>