title: NSLog() vs printf() when printing C string (UTF-8)
body: <p>I have noticed that if I try to print the byte array containing the representation of a string in UTF-8, using the format specifier "%s", <code>printf()</code> gets it right but <code>NSLog()</code> gets it garbled (i.e., each byte printed as-is, so for example "¥" gets printed as the 2 characters: "¬•").
  This is curious, because I always thought that <code>NSLog()</code> is just <code>printf()</code>, plus:</p>
  
  <ol>
  <li>The first parameter (the 'format') is an Objective-C string, not a C
  string (hence the "@"). </li>
  <li>The timestamp and app name prepended. </li>
  <li>The newline automatically added at the end. </li>
  <li>The ability to print Objective-C objects (using the format "%@").</li>
  </ol>
  
  <p>My code:</p>
  
  <pre><code>NSString* string; 
  
  // (...fill string with unicode string...)
  
  const char* stringBytes = [string cStringUsingEncoding:NSUTF8Encoding];
  
  NSUInteger stringByteLength = [string lengthOfBytesUsingEncoding:NSUTF8Encoding];
  stringByteLength += 1; // add room for '\0' terminator
  
  char* buffer = calloc(sizeof(char), stringByteLength);
  
  memcpy(buffer, stringBytes, stringByteLength);
  
  NSLog(@"Buffer after copy: %s", buffer);
  // (renders ascii, no matter what)
  
  printf("Buffer after copy: %s\n", buffer);
  // (renders correctly, e.g. japanese text)
  </code></pre>
  
  <p>Somehow, it looks as if <code>printf()</code> is "smarter" than <code>NSLog()</code>. Does anyone know the underlying cause, and if this feature is documented anywhere? (Couldn't find)</p>