Re: NUL characters in NSString cause unexpected results
Re: NUL characters in NSString cause unexpected results
- Subject: Re: NUL characters in NSString cause unexpected results
- From: Chris Suter <email@hidden>
- Date: Thu, 23 Nov 2006 15:00:31 +1100
That first line also drew a couple of complier warnings:
warning: hex escape sequence out of range
warning: non-ASCII character in CFString literal
You get the first warning because it's interpreted it as \x80e and
that's why the e disappeared.
You got the second warning because you're doing something illegal.
You can't specify a UTF8 string literal.
You need to put the code back to more like you originally had it:
NSString *s1 = [[NSString alloc] initWithBytes:"A\xc0\x80" "end"
length:6
encoding:NSUTF8StringEncoding];
However, I tried this and it doesn't work. The sequence 0xc0 0x80
isn't strictly legal UTF-8 but it is used in some circumstances to
represent U-0000.
I modified your code slightly, and this did appear to work:
unsigned short buffer[] = { 'A', 0, 'e', 'n', 'd' };
NSString* s1 = [[NSString alloc]
initWithBytes:buffer
length:sizeof (buffer)
encoding:CFStringConvertEncodingToNSStringEncoding
(kCFStringEncodingUTF16LE)] ; // @"A[NUL]end" (*)
Nslog(@"s1 = %@", s1) ;
NSString* s2 = @"CD" ;
NSLog(@"s2 = %@", s2) ;
NSString* sC = [s1 stringByAppendingString:s2] ;
NSLog(@"sC = %@", sC) ;
NSLog(@"length of s1:%i", [s1 length]) ;
NSLog(@"length of s2:%i", [s2 length]) ;
NSLog(@"length of sC:%i", [sC length]) ;
[s1 release] ;
Note that you need to modify it accordingly if you're running on a
PowerPC machine. (It just occurred to me that I could have made the
first character 0xfeff and used NSUnicodeStringEncoding and it would
work on both architectures.)
- Chris
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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