Monthly Archives: November 2009

Here’s how you do it.

NSString to char*

NSString* nsstr = @"My NSString" ;
const char * cstr = [ nsstr cStringUsingEncoding:ASCIIEncoding ] ;

// There's also
const char * cstr2 = [ nsstr UTF8String ] ;

These are covered in the docs page for NSString, basically.

char * to NSString

// Here you simply want to use one of the static method constructors
const char * cstyleString = "HELLO!!" ;
NSString * nsstr = [ NSString stringWithUTF8String:cstyleString ] ;

Or an instance method
NSString * nsstr2 = [[ NSString alloc ] initWithUTF8String:cstyleString ]

(( just a note that any methods labelled with + method are static methods and methods labelled with - are instance methods ))

sprintf() for NSString

Use initWithFormat

[ [ NSString alloc ] initWithFormat:@"%s is %d years old", "Bobby", 45 ]