转载自:http://ningmengjiabing.blog.163.com/blog/static/2048471982013022356493/
NSString转换为char*: 代码如下:
1: NSString *string_content = "I'm jordy";
2: char *char_content = [string_content cStringUsingEncoding:NSASCIIStringEncoding];
char*转换为NSString类型的代码如下:
1: char char_array[1024];
2: NSString *string_content = [[NSString alloc] initWithCString:(const char*)char_array
encoding:NSASCIIStringEncoding];
实例:将NSString类型的16进制数转为int类型。
步骤:
1、将NSString类型转换为char类型;
2、通过sscanf()函数得到int类型16进制数的值;
代码:
int color; NSString *color_string = @"0xff0000"; char *color_char = [color_string cStringUsingEncoding:NSASCIIStringEncoding]; sscanf(color_char,"%x",&color); NSLog(@"color: %i",color);