Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thevibesman

macrumors regular
Original poster
Oct 26, 2007
139
0
I'm having unexpected Log output on the following code and I'm guessing it is because I'm a little confused about NSString -> char[]

Code:
NSString *myString = [[NSString alloc] init];
myString = @"ABCDEFGHIJK";
int addressLength = [myString = length];
char buffer[addressLength];
[myString getCharacters:buffer];

	NSLog([@"address for buffer: " stringByAppendingString:[NSString stringWithCharacters:address length:addressLength]]);
	
	NSLog([@"buffer[]: " stringByAppendingString:[NSString stringWithCharacters:buffer length:(11)]]);
	NSLog([@"0: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[0]]]);
	NSLog([@"1: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[1]]]);
	NSLog([@"2: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[2]]]);
	NSLog([@"3: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[3]]]);
	NSLog([@"4: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[4]]]);
	NSLog([@"5: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[5]]]);
	NSLog([@"6: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[6]]]);
	NSLog([@"7: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[7]]]);
	NSLog([@"8: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[8]]]);
	NSLog([@"9: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[9]]]);
	NSLog([@"10: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[10]]]);
	NSLog([@"11: " stringByAppendingString:[NSString stringWithFormat:@"%c", buffer[11]]]);

I get the following output:
2008-12-19 21:30:43.914 NetworkTests[43069:20b]
buffer[]: ABCDEFGHIJK
2008-12-19 21:30:43.915 NetworkTests[43069:20b] 0: A
2008-12-19 21:30:43.916 NetworkTests[43069:20b] 1:
2008-12-19 21:30:43.916 NetworkTests[43069:20b] 2: B
2008-12-19 21:30:43.917 NetworkTests[43069:20b] 3:
2008-12-19 21:30:43.917 NetworkTests[43069:20b] 4: C
2008-12-19 21:30:43.918 NetworkTests[43069:20b] 5:
2008-12-19 21:30:43.918 NetworkTests[43069:20b] 6: D
2008-12-19 21:30:43.918 NetworkTests[43069:20b] 7:
2008-12-19 21:30:43.919 NetworkTests[43069:20b] 8: E
2008-12-19 21:30:43.919 NetworkTests[43069:20b] 9:
2008-12-19 21:30:43.920 NetworkTests[43069:20b] 10: F

When I would expect:

2008-12-19 21:30:43.914 NetworkTests[43069:20b] buffer[]: ABCDEFGHIJK
2008-12-19 21:30:43.915 NetworkTests[43069:20b] 0: A
2008-12-19 21:30:43.916 NetworkTests[43069:20b] 1: B
2008-12-19 21:30:43.916 NetworkTests[43069:20b] 2: C
2008-12-19 21:30:43.917 NetworkTests[43069:20b] 3: D
2008-12-19 21:30:43.917 NetworkTests[43069:20b] 4: E
2008-12-19 21:30:43.918 NetworkTests[43069:20b] 5: F
2008-12-19 21:30:43.918 NetworkTests[43069:20b] 6: G
2008-12-19 21:30:43.918 NetworkTests[43069:20b] 7: H
2008-12-19 21:30:43.919 NetworkTests[43069:20b] 8: I
2008-12-19 21:30:43.919 NetworkTests[43069:20b] 9: J
2008-12-19 21:30:43.920 NetworkTests[43069:20b] 10: K

It seems that for some reason every character in the string is being separated by a '\0' character. Are characters in NSString two byte characters? explaining why when I turn the entire array into a NSString at once it prints correctly?
 

thevibesman

macrumors regular
Original poster
Oct 26, 2007
139
0
Ok, so I read a little more and read that yes the characters in NSString are 16Bit. Knowing that should fix things....ignore this post unless I post again because my fix didn't work.
 

Guiyon

macrumors 6502a
Mar 19, 2008
771
4
Cambridge, MA
If you just want a C string, I'd suggest using
Code:
cStringUsingEncoding:(NSStringEncoding)encoding
and pass it NSASCIIStringEncoding as the encoding type. This may return nil if there are multibyte unicode characters in the input; the function fails if it cannot perform a lossless conversion.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Don't use ASCII unless you're 100% guaranteed the string is in that format. Use the UTF8String method which is much safer.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.