开发者

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

开发者 https://www.devze.com 2023-03-21 13:23 出处:网络
Does anyone know How to convert ISO-8859-1开发者_JS百科 encoded string into UTF-8 string or into NSString in Objective C ?

Does anyone know How to convert ISO-8859-1开发者_JS百科 encoded string into UTF-8 string or into NSString in Objective C ?

thanks.


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

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

Note: Latin-1 and ISO-8859-1 encoding are the same.

With the following code, you can convert it to a UTF-8 encoded C string if needed:

const char* utf8String = [str UTF8String];


Or in one line:

NSString yourFinalString = [NSString stringWithCString:[yourOriginalString cStringUsingEncoding:NSISOLatin1StringEncoding] encoding:NSUTF8StringEncoding];
0

精彩评论

暂无评论...
验证码 换一张
取 消