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[]
I get the following output:
2008-12-19 21:30:43.914 NetworkTests[43069:20b]
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?
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?