用 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但它仍然不会打印
*更新找到了答案
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一种方式:
An other way :
我发现问题是 %C 适用于 16 位 unichar,所以如果我想附加到 NSString,我必须使用 %c,它是 8 位。这非常有效。
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.