Convert NSString to char array

I have a variable of type char[] , and I want to copy the NSString value into it. How to convert an NSString array to a char array?

+43
type-conversion objective-c
Aug 27 '10 at 5:26
source share
6 answers

Use -[NSString UTF8String] :

 NSString *s = @"Some string"; const char *c = [s UTF8String]; 

You can also use -[NSString cStringUsingEncoding:] if your string is encoded with something other than UTF-8.




Once you have const char * , you can work with it similarly to the chars array:

 printf("%c\n", c[5]); 

If you want to change the line, make a copy:

 char *cpy = calloc([s length]+1, 1); strncpy(cpy, c, [s length]); // Do stuff with cpy free(cpy); 
+87
Aug 27 '10 at 5:30
source share

Mipadi's answer is best if you just want char* contain the contents of the string, however NSString provides methods to get the data into the buffer that you yourself allocated. For example, you can copy characters to a unichar array using getCharacters:range: as follows:

 NSUInteger length = [str length]; unichar buffer[length]; [str getCharacters:buffer range:NSMakeRange(0, length)]; for (NSUInteger i = 0; i < length; i++) { doSomethingWithThis(buffer[i]); } 

If you need to use char , you can use the more complex getBytes:maxLength:usedLength:encoding:options:range:remainingRange: as shown (shown in the Eastern Polish tree notation):

 NSUInteger length = [str length]; NSUInteger bufferSize = 500; char buffer[bufferSize] = {0}; [str getBytes:buffer maxLength:(bufferSize - 1) usedLength:NULL encoding:NSUTF8StringEncoding options:0 range:NSMakeRange(0, length) remainingRange:NULL]; 
+9
Aug 27 '10 at 6:00
source share
 NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i < [string length]; i++) { [array addObject:[NSString stringWithFormat:@"%C", [string characterAtIndex:i]]]; } 
+9
Dec 21 '12 at 16:32
source share

Instead of getCharacters:range: I use:

 [stringToCopy getCString:c_buffer maxLength:c_buffer_length encoding:NSUTF8StringEncoding]; 

The result is char [] (instead of unichar [] ), which the OP wants and what you probably want to use for C.

+3
Jan 28 2018-11-11T00:
source share

We need to play NSString as a character array to work on encoding methods, which otherwise would be much easier to do with simple C encoding. Using characterAtIndex: and appendFormat: helps me. Maybe this will help.

 NSString *str = @"abcdef"; NSMutableString *strResult = [NSMutableString string]; for (NSUInteger i = 0; i < [str length]; i++) { char ch = [str characterAtIndex:i]; [strResult appendFormat:@"%c", ch]; } NSLog(@"%@", strResult); 
+1
Mar 19 '17 at 7:43 on
source share

In Swift, the char array is joined as UnsafePointer<Int8> . Character access works the same in Swift for NSString :

 let str: NSString = "hello" let charStr = str.UTF8String // UnsafePointer<Int8> 

For the Swift String object, everything is a little different:

 let str = "hello" let charStr = str.cStringUsingEncoding(NSUTF8StringEncoding) 

charStr [CChar]? where CChar is typeailais for Int8 .

0
Mar 18 '16 at 19:13
source share



All Articles