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: Preston Sumner <email@hidden>
- Date: Thu, 18 Aug 2011 14:14:14 -0600
On Aug 18, 2011, at 2:02 PM, Wilker wrote:
> Hi guys,
>
> I'm having some trouble by trying to convert a C String to NSString, I'm
> currently using:
>
> NSString *mystring = [NSString stringWithUTF8String:cstring];
>
> 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.
>
> What you guys suggest in order that I can convert it to NSString, even
> losing non-ascii chars, but keeping others? Or a good way to guess the
> encoding and convert property?
Apple describes a process of initializing strings with unknown encodings: http://developer.apple.com/library/iOS/#documentation/Cocoa/Conceptual/Strings/Articles/readingFiles.html
Basically, you try the most likely encodings first, and you keep going through a list of possible encodings until you get something you can work with--or something you assume you can work with, anyway. Without any information about what the encoding is likely to be, you're just guessing.
Preston_______________________________________________
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