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:

http://www.positivespinmedia.com/dev/TEC.zip

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]
unsignedIntValue]];
i++;
}

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

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

John


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
> http://www.nemesys-soft.com/
> 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:
>>
>> http://developer.apple.com/library/mac/#documentation/Carbon/Reference/Text_Encodin_sion_Manager/Reference/reference.html%23//apple_ref/doc/uid/TP30000123
>>

_______________________________________________

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)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/maillists%40codeha.us

This email sent to maillists@xxxxxxxxx



Privacy