Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
926 views
in Technique[技术] by (71.8m points)

iphone - Unicode Character from Code in String (Obj-C)

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);

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);

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...