我的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