10

I have seen questions in stackoverflow that convert unichar to NSString but now I would like to do the reverse.

How do i do it?

Need some guidance.. Thanks

For example, I have an array of strings:[@"o",@"p",@"q"];

These are strings inside. How do i convert it back to unichar?

lakshmen
  • 28,346
  • 66
  • 178
  • 276
  • You mean an array of `unichar`? There's a method on `NSString` that will do that. – jscs Mar 06 '13 at 17:38
  • Possible duplicate of [this question](http://stackoverflow.com/questions/1354388/unichar-and-nsstring-how-interchangeable-are-these) – bdesham Mar 06 '13 at 17:39

2 Answers2

13

The following will work as long as the first character isn't actually two composed characters (in other words as long as the character doesn't have a Unicode value greater than \UFFFF):

unichar ch = [someString characterAtIndex:0];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
2

You could convert it to a buffer in NSData:

if ([string canBeConvertedToEncoding:NSUnicodeStringEncoding]) {
    NSData * data = [string dataUsingEncoding:NSUnicodeStringEncoding];
    const unichar* const ptr = (const unichar*)data.bytes;
    ...
}
justin
  • 104,054
  • 14
  • 179
  • 226