NSLog()
and stringWithFormat:
seem to expect the string for %s
in the "system encoding" (for example "Mac Roman" on my computer):
NSString *string = @"¥";
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(CFStringGetSystemEncoding());
const char* stringBytes = [string cStringUsingEncoding:enc];
NSString *log = [NSString stringWithFormat:@"%s", stringBytes];
NSLog(@"%@", log);
// Output: ¥
Of course this will fail if some characters are not representable in the system encoding. I could not find an official documentation for this behavior, but one can see that using %s
in stringWithFormat:
or NSLog()
does not reliably work with arbitrary UTF-8 strings.
If you want to check the contents of a char
buffer containing an UTF-8 string, then
this would work with arbitrary characters (using the boxed expression syntax to create an NSString
from a UTF-8 string):
NSLog(@"%@", @(utf8Buffer));