用unichar追加String

时间:2011-10-18 11:02:27

标签: iphone objective-c byte nsdata unichar

我在这里遇到了一些奇怪的问题。代码如下

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 

appendString打印,但unicodeString永远不会打印。这是因为字节问题??我试过保留appendString但它仍然不会打印

* UPDATED找到答案

2 个答案:

答案 0 :(得分:3)

另一种方式:

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

答案 1 :(得分:2)

我发现问题是%C是16位unichar所以如果我想附加到NSString我必须使用%c,这是8位。这非常有效。

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