Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
- Subject: Re: NSString encoding problem with extended characters ( AKA : converting NSString to std::string)
- From: Ricky Sharp <email@hidden>
- Date: Mon, 19 Mar 2007 20:19:47 -0500
On Mar 19, 2007, at 7:54 PM, Alexander Hartner wrote:
I am obtaining a NSString from the iSync API via :
[changeRecord objectForKey:@"first name"]
and I am trying to convert this string into a std::string. I tried
using :
std::string * firstName = new std::string([[changeRecord
objectForKey:@"first name"] UTF8String]);
which seems to work in most cases, however sometime the string
contains characters from the extended character set (>127). I
managed to obtain on of these strings and encode is using
encoding:NSNonLossyASCIIStringEncoding and ended up with Bj\366rn
instead of Björn. After some poking around and trying out many of
the other encoding I ended up with the examples at the bottom of
this message.
NSNonLossyASCIIStringEncoding uses ASCII7 characters to represent all
possible Unicode code points. For all characters with a value above
0x0075, it will use an escaped sequence to represent that char.
I then put together a test program (below) which ended up showing
that the string does not support any of the available encoding.
This leads me to questions how can I convert the String obtained
from the iSync API to a std::string in the correct encoding. I
checked the address book and made sure that Björn was entered using
the same keyboard combination as the commented line below, to
ensure that it's the same character obtained from the literal
string and the iSync API. For some reason it didn't work.
[rest snipped]
Why do you need the data to end up in a std::string? The NSString
you start with is UTF-16. From there, if you want to save it to a
file (I'm guessing UTF-8), you can use
writeToFile:atomically:encoding:error:
If you choose to use an encoding other than UTF-xx, you'll end up
potentially mangling the data.
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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