开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜