iphone ,how to print the char array
I have a char array,char contract[8];and assign th开发者_JS百科e value to array,
and I want to print the value,so I use NSLog(@"%@",contract);
and build succeeded.but run incorrect.
Try
NSLog(@"%@",[NSString stringWithCString:contract encoding:NSUTF8StringEncoding]);
Basically, you need to make the C string an NSString object.
If you know the size
NSLog(@"%.*s", 8, contract);
If contract is a NULL-terminated string
NSLog(@"%s", contract);
or just convert to NSString
NSLog(@"%@", [[[NSString alloc] initWithBytesNoCopy:contract length:contractLen encoding:NSASCIIStringEncoding freeWhenDone:NO] autorelease]);
for (Char *string in myArray) {
NSLog(@"%@", string);
}
Please go through Format Specifiers. Try %c
instead of %@
. Hope this helps
精彩评论