字符串中的代码的Unicode字符(Obj-C) [英] Unicode Character from Code in String (Obj-C)

查看:72
本文介绍了字符串中的代码的Unicode字符(Obj-C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在NSString中有一个unicode十六进制值 - 如何输出该字符;这就是我所拥有的:

I have a unicode hex value in an NSString - how do I output the character; here's what I have:

NSLog(@"\U0001D000");

NSMutableString *hexString = [[NSMutableString alloc] initWithString:@"0001D000"];
[hexString insertString:@"\\U" atIndex:0];
NSLog(@"%@", hexString);

第一个NSLog输出字符;第二个只产生输出\ U0001D000

The first NSLog outputs the character; the second just produces the output "\U0001D000"

我尝试了很多组合而且不知所措 - 例如,我试过了

I've tried lots of combinations and am at a loss - for example, I tried

NSLog(@"\U%@", hexString);

但这会产生编译器错误,因为它在\U之后寻找一串数字

But this gives a complier error, as it is looking for a string of numbers after the \U

推荐答案

如果您的角色需要代理对(U + 10000到U + 10FFFF),请使用 CFStringGetSurrogatePairForLongCharacter 将Unicode代码点转换为UTF-16代理对,然后 -initWithCharacters:length:将其转换为NSString。例如:

If your character requires a surrogate pair (U+10000 to U+10FFFF), use CFStringGetSurrogatePairForLongCharacter to convert the Unicode code point into a UTF-16 surrogate pair, and then -initWithCharacters:length: to convert it into an NSString. For example:

UniChar c[2];
CFStringGetSurrogatePairForLongCharacter(0x1D000, c);
NSString *s = [[NSString alloc] initWithCharacters:c length:2];

对于其他字符( CFStringGetSurrogatePairForLongCharacter 返回 FALSE ),你可以跳过转换并直接进入 -initWithCharacters:length:

For other characters (CFStringGetSurrogatePairForLongCharacter returns FALSE), you can skip the conversion and go straight to -initWithCharacters:length:.

这篇关于字符串中的代码的Unicode字符(Obj-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆