FROM : Stuart Malin
DATE : Tue Mar 04 08:00:59 2008
My problem is that I receive a function call from a C library that
gives me a wchar_t array and its length. The unicode array is _not_
terminated.
The library defines an XML_Char type, so my code below refers to
that, but XML_Char is wchar_t (which, I believe is UTF8 on a Mac).
I'm very weak with C, so please forgive my perhaps naive attempts here.
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];
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
DATE : Tue Mar 04 08:00:59 2008
My problem is that I receive a function call from a C library that
gives me a wchar_t array and its length. The unicode array is _not_
terminated.
The library defines an XML_Char type, so my code below refers to
that, but XML_Char is wchar_t (which, I believe is UTF8 on a Mac).
I'm very weak with C, so please forgive my perhaps naive attempts here.
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];
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 mail archive

