• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How to get an NSString from a non-terminated array of unicode chars (length is known)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to get an NSString from a non-terminated array of unicode chars (length is known)


  • Subject: How to get an NSString from a non-terminated array of unicode chars (length is known)
  • From: Stuart Malin <email@hidden>
  • Date: Mon, 3 Mar 2008 21:00:59 -1000

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 (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


  • Follow-Ups:
    • Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
      • From: Dave Camp <email@hidden>
    • Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
      • From: Jens Alfke <email@hidden>
    • Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
      • From: Karsten <email@hidden>
  • Prev by Date: Any Seattle-area Cocoa developer groups?
  • Next by Date: Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
  • Previous by thread: Re: Any Seattle-area Cocoa developer groups?
  • Next by thread: Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
  • Index(es):
    • Date
    • Thread