开发者

iphone ,how to print the char array

开发者 https://www.devze.com 2023-04-05 19:41 出处:网络
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

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

精彩评论

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

关注公众号