Re: Convert between encodings
Re: Convert between encodings
- Subject: Re: Convert between encodings
- From: Alastair Houghton <email@hidden>
- Date: Wed, 6 Dec 2006 18:34:50 +0000
On 6 Dec 2006, at 18:19, malcom wrote:
Suppose to have some data written to a file (this data was taken using
NSISOLatin1StringEncoding and then saved to file).
Now I need to get it (NSData or NSString) and convert to
NSUTF8StringEncoding (it contains an header that tell me what kind of
encoding is used).
How can I need to proceed?
[snip]
What's the way to convert an NSString (that basically works with
unicode)
from one encoding to another?
You can't. NSString is always Unicode, at least from your
perspective as a user of the NSString API. (The internal
representation might be something different, but you'll never see that.)
If your data is presently in ISO Latin 1, you need to read it in
using something like
NSString *str = [[[NSString alloc] initWithData:data
encoding:NSISOLatin1StringEncoding]
autorelease];
If you then need UTF8, then you need to do
[str UTF8String]
which returns a const char * pointer, or
[str dataUsingEncoding:NSUTF8StringEncoding]
which will give you an NSData containing UTF-8 encoded characters.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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