Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert charset name to NSStringEncoding

Given a charset string, such as "utf-8", "iso-8859-1", "us-ascii" etc, is there any built-in way to get the appropriate NSStringEncoding in Cocoa?

Right now I'm looking at just building a NSDictionary containing a canonicalized version of the name mapped to the NSStringEncoding, then having a lookup mechanism that canonicalizes the input in the same way. But is there really no way to get NSUTF8StringEncoding given the string "UTF-8", etc?

like image 962
d11wtq Avatar asked May 23 '10 10:05

d11wtq


1 Answers

You have to go through CoreFoundation.

CFStringConvertIANACharSetNameToEncoding
CFStringConvertEncodingToNSStringEncoding

First converting the IANA Char set to CFStringEncoding value (Which is not the same as NSStringEncoding) and then convert the CFStringEncoding to NSStringEncoding.

NSString *encodingString = @"utf-8"
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef) encodingString));
like image 68
drawnonward Avatar answered Sep 21 '22 08:09

drawnonward