Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting NSString to unichar in iOS

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?

like image 367
lakshmen Avatar asked Mar 06 '13 17:03

lakshmen


2 Answers

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];
like image 69
rmaddy Avatar answered Nov 18 '22 04:11

rmaddy


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;
    ...
}
like image 26
justin Avatar answered Nov 18 '22 04:11

justin