Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert cstring to NSString and NSString to cstring?

Tags:

c

objective-c

Lets say i have the following cstring

char array[1000];

How i can convert it to NSString and vice verse.

Thanks.

like image 303
itsaboutcode Avatar asked Feb 14 '26 13:02

itsaboutcode


1 Answers

Apple's Developer Reference has a good article on this subject. Basically, you will do something like this:

NSString *stringFromUTFString = [[NSString alloc] initWithUTF8String:utf8String];

if the string is UTF8 encoded. Otherwise, you can use initWithCString:encoding: with which you can specify the encoding.

Here is a list of available string encodings.

like image 51
Jorge Israel Peña Avatar answered Feb 16 '26 02:02

Jorge Israel Peña