Search code examples
iphoneobjective-cbytensdataunichar

Append String with unichar


I got some weird issue here. The code is as below

NSMutableString *unicodeString = [NSMutableString string];
for (NSUInteger i = 0; i < [data length]; i++) {
    unsigned char byte;
    [data getBytes:&byte range:NSMakeRange(i, 1)];
    unichar unicodeChar = byte;
    NSString *appendString = [NSString stringWithFormat:@"%C",[_toUnicode unicharFromCIDString:unicodeChar]];
    [unicodeString appendFormat:@"%@",appendString];
    NSLog(@"%@",appendString); //1
}
NSLog(@"%@",unicodeString)//2 

the appendString print, but unicodeString never print. Is this because of bytes issue?? I have tried retain appendString but it still won't print

*UPDATED found the answer


Solution

  • I have found that the problem is %C is for 16bit unichar so If I want to append to NSString I have to use %c which is 8bit. This works perfectly.

    NSString *appendString = [NSString stringWithFormat:@"%c",[_toUnicode     unicharFromCIDString:unicodeChar]];
    [unicodeString appendFormat:@"%@",appendString];