tags:

views:

3896

answers:

2

I have been able to find methods like -[NSString stringWithCString:encoding:] but they do not seem to play well when the cstring is a pointer.

+8  A: 

stringWithCString:encoding: creates an NSString from a given C string. To create a C string from an NSString, use either the UTF8String method (generally preferred) or cStringUsingEncoding: (if you need an encoding other than UTF-8).

Chuck
Since the cString method is deprecated, this answer is better than the accepted one.
floehopper
yeah my bad, i've fixed this now
Brock Woolf
+2  A: 

First up, don't use initWithCString, it has been deprecated.

Couple of ways you can do this:

const *char cString = "Hello";
NSString *myNSString = [NSString stringWithUTF8String:cString];

If you need another encoding like ASCII:

const *char cString = "Hello";
NSString *myNSString = [NSString stringWithCString:cString encoding:NSASCIIStringEncoding];

If you want to see all the string encodings available, in Xcode, hold command + option then double click on NSASCIIStringEncoding in the above code block.

You will be able to see where Apple have declared their enumeration for the string encoding types. Bit quicker than trying to find it in the documentation.

Some other ones you might need:

NSASCIIStringEncoding
NSUnicodeStringEncoding // same as NSUTF16StringEncoding
NSUTF32StringEncoding

Checkout Apple's NSString Class Reference (encodings are at the bottom of the page)

Brock Woolf