Re: (Resolved) How to get an NSString from a non-terminated array of unicode chars (length is known)
Re: (Resolved) How to get an NSString from a non-terminated array of unicode chars (length is known)
- Subject: Re: (Resolved) How to get an NSString from a non-terminated array of unicode chars (length is known)
- From: Stuart Malin <email@hidden>
- Date: Tue, 4 Mar 2008 19:25:10 -1000
Thank you everybody for your thoughts and suggestions.
I hope some of the conversation that was stirred up was useful.
NSString's initWithBytes:len:encoding: worked.
It did so because I had a misunderstanding -- in most cases my source
string is UTF-8 (not UTF-16), and the source string is byte wide, not
wchar_t.
Howvere, I do have cases where the content passes to me is not valid
UTF-8, and further contains a non-7-bit ASCII character which is
actually a separator character used to distinguish a set of joined
UTF-8 strings... so I wrote a routine to scan this byte sequence,
identify start and end points, and then use NSString's
initWithBytes:len:encoding: to encode the sections.
Again, many thanks for the contributions made to help me.
--Stuart
On Mar 4, 2008, at 5:01 PM, Deborah Goldsmith wrote:
You don't need the NSMutableData:
NSString *str = [[[NSString alloc] initWithBytes:s length:len
encoding:NSUTF8StringEncoding] autorelease];
...assuming it's really UTF-8. If it's wchar_t and you know it's
Unicode, use NSUTF32StringEncoding (only available on 10.5 or
later). If it's UTF-16, use stringWithCharacters:length:.
Deborah Goldsmith
Apple Inc.
email@hidden
On Mar 3, 2008, at 11:44 PM, Stuart Malin wrote:
Yes - thanks - that works:
NSMutableData *data = [NSMutableData dataWithBytes:(void *)s
length:len];
//append a NULL unicode char
XML_Char nullChar = 0;
XML_Char *nullCharPtr = &nullChar;
int nullCharLen = sizeof nullChar;
[data appendBytes:nullCharPtr length:nullCharLen];
NSString *str = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
_______________________________________________
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