NSString -> char[] confusion

Discussion in 'iPhone/iPad Programming' started by thevibesman, Dec 19, 2008.

  1. macrumors regular

    Joined:
    Oct 26, 2007
    #1
    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]
    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?
     
  2. thread starter macrumors regular

    Joined:
    Oct 26, 2007
    #2
    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.
     
  3. macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #3
    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.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Don't use ASCII unless you're 100% guaranteed the string is in that format. Use the UTF8String method which is much safer.
     

Share This Page