4

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

Lunayo
  • 538
  • 7
  • 32
  • Could you edit your question to show the unicharFromCIDString function? I'm currently thinking this is either a NSString encoding issue or the bytes are just out of range of what the encoding can handle printing out. – Michael Dautermann Oct 18 '11 at 11:08

2 Answers2

3

An other way :

NSString *appendString = [NSString stringWithCharacters:&unicodeChar length:1];
[unicodeString appendFormat:@"%@", appendString];
csblo
  • 1,571
  • 13
  • 20
2

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];
Lunayo
  • 538
  • 7
  • 32