Converting Between C and Objective-C Strings (NSString)

Given the foundation of Objective-C is built upon the C programming language, it’s good to know how to convert C based strings to NSString objects, and vice-versa.

For clarification: A C string is simply a series of characters (a one-dimensional character array) that is null-terminated, whereas an NSString object is a complete object with class methods, instance methods, etc.

The first code block below shows how to create an NSString object and convert it to a C string. The second example shows a C string converted to an NSString object.

// NSString object to C
NSString *str = @"String object to a C string";
const char *ptr = [str cStringUsingEncoding:NSUTF8StringEncoding];
printf("%s\n", ptr);
 
// Another approach with the same result
const char *ptr2 = [str UTF8String];
printf("%s\n", ptr2);
 
// C string to NSString object
ptr = "C string to string object";
str = [NSString stringWithCString:ptr encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);
  1. I’m pretty sure using the NSASCIIStringEncoding would be more faithful to the C-string type.

  2. Who is responsible for freeing the memory associated with the C string in each case? Does the NSString object still manage it, or does the caller with the pointer need to deal with it?

    • The C string returned string is autoreleased, you would need to create a copy of the C string if you need it outside the current context.

  3. How much is the memory allocated for same C String and NSString?

    Do using C String makes the use of less memory , which would be better for mobile applications. Please clarify me on this one.

    Thank you.

    • Whenever possible, I would recommend NSString objects versus C strings as there is much more flexibility in working with objects. Typically C strings are used if you work with an API that deals with C strings, thus the need to convert between these types becomes necessary.

      • Thanks John, Agreed.

        Any idea on difference of the memory allocated over the RAM (not sure of the terminology over mobile device, to be clear , its regarding volatile memory ) of the device, for same content of C String and NSString.

        AFAIK, C string as a data-type would be taking a less memory when compared to NSString which is a class object containing methods and member values. Just to confirm once..

        • Yes, the C string should take less memory as it is stored only as an array of characters, where there would be more overhead for an NSString object.

  4. NSString.h has the following comment:

    NSASCIIStringEncoding = 1, /* 0..127 only */

    NSUTF8StringEncoding has no similar comments.

    It might be reasonable to conjecture that NSASCIIStringEncoding is only intended to be used with the lower-half of the one-byte character set. In other words, NSASCIIStringEncoding is only valid for ASCII characters (in the range of 0 to 127).

    C strings really are only ASCII; however, the language doesn’t enforce this, and it is quite common to see encryption code that uses unsigned char to store encrypted data, which frequently has the high bit set.

    Basically, the difference is semantic, and I suspect that it doesn’t make any difference if you are actually using the standard ASCII character set. However, I have not verified this; if you’re project has a critical reliance on this data, don’t take my opinion as read — do the research!

  5. As of Xcode 4.5, you can create NSStrings from C strings using literal syntax. E.g.

    const char *cstring = “C string to string object”;
    NSString *string = @(cstring);

    (Works on iOS 4+ and Mac OS 10.6.8+)

Comments are closed.