我的Unicode字符的,我想“代码”列表使用\u
转义序列(如\ue415
)打印,只要我努力像这样的东西来撰写它:撰写的Unicode字符格式的NSString
// charCode comes as NSString object from PList
NSString *str = [NSString stringWithFormat:@"\u%@", charCode];
编译器警告我关于不完整的字符代码。任何人都可以帮助我完成这个简单的任务吗?
我的Unicode字符的,我想“代码”列表使用\u
转义序列(如\ue415
)打印,只要我努力像这样的东西来撰写它:撰写的Unicode字符格式的NSString
// charCode comes as NSString object from PList
NSString *str = [NSString stringWithFormat:@"\u%@", charCode];
编译器警告我关于不完整的字符代码。任何人都可以帮助我完成这个简单的任务吗?
我认为你不能这样做你试图 - \ uxxx转义序列用于表明一个常量是一个Unicode字符 - 并且该转换在编译时处理。
你需要的是你的则charCode转换成整数,并使用该值作为格式参数:
unichar codeValue = (unichar) strtol([charCode UTF8String], NULL, 16);
NSString *str = [NSString stringWithFormat:@"%C", charCode];
NSLog(@"Character with code \\u%@ is %C", charCode, codeValue);
对不起,那NUST不能摆脱十六进制表示int值的最好方式,但是这即来到第一想到
编辑:看来,NSScanner
类可以在十六进制表示扫描NSString
对数:
unichar codeValue;
[[NSScanner scannerWithString:charCode] scanHexInt:&codeValue];
...
请注意,并非所有字符都可以用UTF-8编码。昨天我遇到了一些错误,其中有些韩文字符未能正确编码为UTF-8。
我的解决方案是将格式字符串从%s更改为%@并避免重新编码问题,尽管这可能对您不适用。
基于从@Vladimir代码,这个工作对我来说:
NSUInteger codeValue;
[[NSScanner scannerWithString:@"0xf8ff"] scanHexInt:&codeValue];
NSLog(@"%C", (unichar)codeValue);
不是由领导 “\ U” 或 “\\ U”,从API文档:
The hexadecimal integer representation may optionally be preceded
by 0x or 0X. Skips past excess digits in the case of overflow,
so the receiver’s position is past the entire hexadecimal representation.
你想用给定的代码输出unicode符号? – Vladimir 2011-01-06 13:54:38
是的,我已经尝试'%C'占位符没有运气。 – Eimantas 2011-01-06 14:05:24