• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to detect string encoding before reading a file in NSString?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: Re: How to detect string encoding before reading a file in NSString?
  • From: Greg Guerin <email@hidden>
  • Date: Tue, 26 Apr 2011 13:41:14 -0700

Laurent Daudelin wrote:

I've found different ways to do that (some pure Cocoa, some using Carbon) but I was wondering about the wisdom of this list as to what is the best way to detect the encoding of a file before passing it to NSString initWithContentsOfFile:encoding:error:?


You might not have to guess at all, if the file has an xattr of "com.apple.TextEncoding" attached to it. I don't know if any of the NSString encoding-guessing methods use this xattr or not. It's easy to test, though: TextEdit.app writes the xattr when you Save As and choose an encoding.

The contents of the xattr is a text-encoding name (such as "utf-8"), then a semicolon, then a longish decimal number.

You can see a file's xattrs with the xattr command in Terminal. Type 'xattr -h' for a summary of options and args. The xattr API works great in C (BSD functions, exist since OS 10.4 Tiger).

Google search terms:
xattr site:developer.apple.com

  -- GG

_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


  • Prev by Date: Re: How to detect string encoding before reading a file in NSString?
  • Next by Date: iOS Best Practice Question
  • Previous by thread: Re: How to detect string encoding before reading a file in NSString?
  • Next by thread: iOS Best Practice Question
  • Index(es):
    • Date
    • Thread