• 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: Safe way to convert C String to NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Safe way to convert C String to NSString


  • Subject: Re: Safe way to convert C String to NSString
  • From: Jens Alfke <email@hidden>
  • Date: Thu, 18 Aug 2011 13:19:29 -0700

On Aug 18, 2011, at 1:02 PM, Wilker wrote:

> But I don't know which encoding the string is using... And when it has some
> latin or other kind of characteres, the return is "nil".
> In my case I really don't care about these characters, if I can just remove
> non-ascii from C String and them convert to NSString will be fine for me.

If you can find out what the actual encoding is, it’s best to use it. But I know there are cases where you don’t know, or the incorrect encoding is used (hello, RSS feeds…)

What I’ve done as a heuristic is to first try UTF-8, as you’re doing; and if that fails (returns nil) fall back to CP-1252 (NSWindowsCP1252StringEncoding). The rationale is that (a) this is the default encoding used by MS Windows, at least in Western countries; (b) it’s a superset of the highly-standard ISO-8859-1 (aka ISO-Latin-1) which is a superset of ASCII; and (c) it defines all 256 code points.

So the result is that the conversion to NSString will never fail; ASCII characters come out correctly; and non-ASCII Latin characters will quite often come out OK.

Unfortunately this approach doesn’t work at all if the source data is not in an 8-bit encoding — like if it’s UTF-16 or one of the old multi-byte Asian encodings. I’m sure there are heuristics to use there, but I don’t know them.

If you’re reading the string from a file or HTTP URL, you can also use -[NSString initWithContentsOfFile/URL:usedEncoding:error:], which will attempt to figure out the correct encoding using clues like the filename extension, leading BOM bytes, etc.

—Jens_______________________________________________

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

  • Follow-Ups:
    • Re: Safe way to convert C String to NSString
      • From: Wilker <email@hidden>
References: 
 >Safe way to convert C String to NSString (From: Wilker <email@hidden>)

  • Prev by Date: Re: Safe way to convert C String to NSString
  • Next by Date: Re: Safe way to convert C String to NSString
  • Previous by thread: Re: Safe way to convert C String to NSString
  • Next by thread: Re: Safe way to convert C String to NSString
  • Index(es):
    • Date
    • Thread