Objective-C Language Encoding and Decoding


Example

// decode
NSString *string = [[NSString alloc] initWithData:utf8Data
                                         encoding:NSUTF8StringEncoding];

// encode
NSData *utf8Data = [string dataUsingEncoding:NSUTF8StringEncoding];

Some supported encodings are:

  • NSASCIIStringEncoding
  • NSUTF8StringEncoding
  • NSUTF16StringEncoding (== NSUnicodeStringEncoding)

Note that utf8Data.bytes does not include a terminating null character, which is necessary for C strings. If you need a C string, use UTF8String:

const char *cString = [string UTF8String];
printf("%s", cString);