How to convert ISO-8859-1 encoded string to UTF-8 in Objective-C

Does anyone know How to convert an encoded ISO-8859-1 string to a UTF-8 string or to NSString in Objective-C?

thanks.

+4
source share
2 answers

Say you have an ISO-8859-1 encoded string in a varaible isoString type const char* , then you can create an NSString instance as follows:

 NSString* str = [[NSString alloc] initWithCString: isoString encoding: NSISOLatin1StringEncoding]; 

Note. The encoding of Latin-1 and ISO-8859-1 is the same.

Using the following code, you can convert it to a C string with UTF-8 encoding if necessary:

 const char* utf8String = [str UTF8String]; 
+5
source

Or in one line:

 NSString yourFinalString = [NSString stringWithCString:[yourOriginalString cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding]; 
+2
source

All Articles