Convert NSString to NSUTF32StringEncoding

847 Views Asked by At

I have an NSString that internally uses UTF16 encoding. I want to covert it to use UTF32 , so that or q both take single index. Currenty takes 2.

How to do this ?. Even if I can convert to some other type from NSString it will work. Bottom line is to have or q take equal number of indexes in an array.

2

There are 2 best solutions below

0
On

Have you tried [[NSString alloc]initWithData:encoding:]?

Example would be:

NSData *data = [sourceStr dataUsingEncoding:NSUTF16StringEncoding];

Create your NSUTF32StringEncoded string from the data above

NSString *encodedStr = [[NSString alloc]initWithData:data encoding:NSUTF32StringEncoding];
0
On

Did you try :

NSData *data = [string dataUsingEncoding:NSUTF32StringEncoding];

NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding];