Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTF-8 encoding to ISO 8859-1 encoding with NSString

I have an application that reads the data in UTF-8 format from the server, but it has to be displayed in ISO 8859-1(Latin-1). Are there any Cocoa APIs to achieve this?

like image 888
Vidya Murthy Avatar asked Dec 29 '10 10:12

Vidya Murthy


1 Answers

You can use NSString's getCString:maxLength:encoding: method, like this:

char converted[([string length] + 1)];
[string getCString:converted maxLength:([string length] + 1) encoding: NSISOLatin1StringEncoding];

NSLog(@"%s", converted);

Once you have this, you can then re-initialize an NSString instance from that same encoding using the stringWithCString:encoding: class method:

NSString *converted_str = [NSString stringWithCString:converted encoding:NSISOLatin1StringEncoding];
like image 157
Jacob Relkin Avatar answered Sep 19 '22 21:09

Jacob Relkin