• 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
Re: 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]

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: Stuart Malin <email@hidden>
  • Date: Mon, 3 Mar 2008 21:44:56 -1000


On Mar 3, 2008, at 9:19 PM, Karsten wrote:

you can simply use: [NSString stringWithUTF8String: s]. or [NSString stringWithCharacters:s length: len].

Can't use the former because s is not terminated.

The latter (with a cast):
NSString *str = [NSString stringWithCharacters:(const unichar *)s length:len];


doesn't work (must be an encoding discrepancy?). Source ASCII "hello" becomes: 敨汬㱯搯愾


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*.

Yeesh -- I'm embarrassed: but of course.

you could have used: [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding].

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


  • Follow-Ups:
    • Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
      • From: Deborah Goldsmith <email@hidden>
References: 
 >How to get an NSString from a non-terminated array of unicode chars (length is known) (From: Stuart Malin <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: Triggering GUI login session from daemon? And reboot.
  • Next by Date: Looking for a person to port a Unix library to Cocoa
  • Previous by thread: Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
  • Next by thread: Re: How to get an NSString from a non-terminated array of unicode chars (length is known)
  • Index(es):
    • Date
    • Thread