用 unichar 追加字符串

发布于 2024-12-10 06:33:38 字数 608 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔嚣张 2024-12-17 06:33:38

另一种方式:

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

An other way :

NSString *appendString = [NSString stringWithCharacters:&unicodeChar length:1];
[unicodeString appendFormat:@"%@", appendString];
Oo萌小芽oO 2024-12-17 06:33:38

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

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文