Re: Safe way to convert C String to NSString
Re: Safe way to convert C String to NSString
- Subject: Re: Safe way to convert C String to NSString
- From: Wilker <email@hidden>
- Date: Thu, 18 Aug 2011 17:22:01 -0300
The Shawn solution worked really well for me :)
In my case I'm hacking an Zip library that I'm using, this zip library has
problems when inside the zip the name of files are using crazy encodings...
In my case, all I need to know about the file name is it extension (this is
why I don't care about just skipping unknown chars, they are not part of
extension, so, don't matter).
I did some hacks on it, using the [NSString stringWithFormat:"%s",
theString] and it worked as I needed :)
Thanks you guys
---
Wilker Lúcio
http://about.me/wilkerlucio/bio
Kajabi Consultant
+55 81 82556600
On Thu, Aug 18, 2011 at 5:19 PM, Jens Alfke <email@hidden> wrote:
>
> 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