Subject: Re: How to detect string encoding before reading a
file in NSString?

Hi Laurent-

Certainly. I've zipped up two classes that wrap some functionality from the
Text Conversion Manager functions. I'm certain I got these from somewhere else
years ago, but failed to find them now. Download at:

Usage looks something like this:

NSInteger i = 0;
NSString *myResultantString = nil;
TECSniffer *sniffer = [[TECSniffer alloc] initWithWebTextEncodings];
NSArray *results = [sniffer sniff:yourNSDataObjectGoesHere];
while(!myResultantString && i < [results count]){
myResultantString = [[NSString alloc]
initWithData:yourNSDataObjectGoesHere encoding:[[results objectAtIndex:i]

// ideally, you've got a legitimate NSString object when the above is

If memory serves, you'll need to link CoreServices into your app to use TEC.
Hope this helps!


On Apr 27, 2011, at 1:18 PM, Laurent Daudelin wrote:

> John,
> Not sure if you read my last email on the topic but using the
> NSAttributedString initWithURL:encoding:documentAttributes: is failing. I
> have read a bit about the Text Conversion Manager but if you had already some
> code you could share, that would spare me reinventing the wheel!
> Thanks in advance!
> -Laurent.
> --
> Laurent Daudelin
> AIM/iChat/Skype:LaurentDaudelin
> Logiciels Nemesys Software
> laurent@xxxxxxxxxxxxxxxx
> On Apr 26, 2011, at 12:39, John Pannell wrote:
>> Hi Laurent-
>> I have an app that collects a lot of text off the web; my string creation
>> algorithm is something like the following:
>> 1. Attempt to create an NSString with NSUTF8StringEncoding.
>> 2. If the string is nil, attempt to create the string using the encoding
>> returned from the server.
>> 3. If string is still nil, ask the Text Encoding Conversion Manager to
>> sniff out the encoding from the data.
>> 3a. This returns an array of likely encodings. For each item in the
>> array:
>> 3b. Attempt to create a string with the encoding.
>> There was a little too much code associated with this to copy/paste into
>> email, but I'd be happy to share... I have a wrapper object for the needed
>> interaction with the Text Encoding Conversion Manager. Some more about it:


Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)

Help/Unsubscribe/Update your Subscription:

This email sent to maillists@xxxxxxxxx