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-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to