Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
- Subject: Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
- From: Karsten <email@hidden>
- Date: Tue, 4 Mar 2008 08:19:15 +0100
hi Stuart,
you can simply use: [NSString stringWithUTF8String: s]. or [NSString
stringWithCharacters:s length: len].
I tried this approach:
NSMutableData *data = [NSMutableData dataWithBytes:(void *)s
length:len]; //as supplied, s is: (const XML_Char *), and len is
its length
//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 stringWithUTF8String:(const char*)data];
NSData is an object, unlike in plain c this doesn't represent a part
of the memory in a given way, instead it hides this information and
provides methods to access the data. So you can't just cast a NSData
object into a const char*.
you could have used: [[NSString alloc] initWithData: data encoding:
NSUTF8StringEncoding].
Kind Regards
Karsten
The idea being to append a NULL to the non-terminated library
supplied wchar_t array, and then convert given its encoding.
But that gives me nil (even though my test data is plain old ASCII
caracters, so all the XML_Chars are single bytes).
So, in experimenting, I tried this:
NSMutableString *ms = [NSMutableString stringWithCapacity:len];
int i;
for (i = 0; i<len; i++) {
[ms appendFormat:@"%C", s[i]];
}
NSLog(@"string is: %@", ms);
That works, but obviously is quite inefficient.
There must be a sensible way to do this?
Many thanks in advance for whatever help someone(s) can provide.
--Stuart
_______________________________________________
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
_______________________________________________
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