Append String with unichar

4.7k views Asked by At

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

2

There are 2 answers

1
Lunayo On BEST ANSWER

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];
0
csblo On

An other way :

NSString *appendString = [NSString stringWithCharacters:&unicodeChar length:1];
[unicodeString appendFormat:@"%@", appendString];