iOS 基本数据类型转换(全网最全)

  • NSString转NSData
  • NSData转NSString
  • BOOL转NSString
  • NSString 转 int
  • NSString 转 float
  • 将基本类型转化成字符串类型-->字符串拼接
  • NSData 转char
  • char 转NSData
  • int转NSNumber
  • NSNumber转int

1.NSString转NSData

NSString * str =@"str"; 
NSData *data =[str dataUsingEncoding:NSUTF8StringEncoding];

2.NSData转NSString

NSString * str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

3.BOOL转NSString

BOOL  change;
  [NSString stringWithFormat:@"%d",change];

4.NSString 转 int(整型)

int  userAnswer = [textField.text  intValue];

5.NSString 转 float(浮点型)

float userAnswer = [textField.text floatValue];

6.将基本类型转化成字符串类型-->字符串拼接

NSString  *name = [NSString   stringWithFormat:@"%d.jpg",count%4];

7.NSData 转char

NSData *data; 
char * haha=[data bytes]; 

8.char 转NSData

byte * byteData = malloc(sizeof(byte)*16); 
NSData *content=[NSData dataWithBytes:byteData length:16];

9.int转NSNumber

NSNumber *numObj = [NSNumber numberWithInt: 2];

10.NSNumber转int

 NSString * string = [NSString stringWithFormat:@"%d",integerNumber]; 

intNum = [string intValue]; 

你可能感兴趣的:(iOS 基本数据类型转换(全网最全))