从Unichars来回转换显示字符的最佳方法?目标C [英] Best way to convert Back and forth from Unichars to display characters? Objective C
问题描述
让我们拿回收符号为例。 U + 267B
我想让我的程序把它转换成实际的符号,并显示出来吗?
然后id喜欢阅读任何符号,并将其转换回Unicode? p>
我期待这是非常简单的,但我想教我这个自我。 ..
谢谢OverFlow !!!!
ps。 MacBook Pro的命令是什么,以输入Unicode,并有相应的符号出现?
这应该为你工作: / p>
NSString * text = inputView.text; //例如\\\♻
NSLog(@%@,text);
//输出:\\\♻
NSData * d = [text dataUsingEncoding:NSASCIIStringEncoding];
NSString *转换= [[NSString分配] initWithData:d编码:NSNonLossyASCIIStringEncoding];
NSLog(@%@,转换);
//输出:♻
outputView.text =转换;
它使用 NSNonLossyASCIIStringEncoding
解码\\ \\ uNNNN到相应的Unicode字符。
要在另一个方向(从♻到\\\♻)进行转换,您只需要
在上面的代码中交换 NSASCIIStringEncoding
和 NSNonLossyASCIIStringEncoding
。
$ b $正如你正确地注意到的那样,反向不能编码在<= U + 00FF范围内的字符
。下面的代码也会转换这些字符:
NSString * text = inputView.text; //例如♻A
NSLog(@%@,text);
//输出:♻A
NSMutableString * converted = [NSMutableString string];
for(NSInteger i = 0; i< [text length]; i ++){
unichar c = [text characterAtIndex:i];
[转换appendFormat:@\\%04X,c];
}
NSLog(@%@,转换);
//输出:\\\♻\\\A
outputView.text =转换;
I am trying to understand Unichars. Coming from Ascii, Unichars just seems like a more advanced and complex library of character codes. Now I am building a simple application that will accept a string "\u####" that will represent the Unicode, that leads me to my first problem. what is the best way to format Unicode, after searching the web for so long, I feel like I have seen many ways to do it. Or maybe I just don't have the best concept of it yet.
Lets take the recycle symbol for example. U+267B
I would like my Program to convert that into the actual symbol, and display it?
Then id like to read in any symbol and convert that back into Unicode?
I am expecting this to be very simple, but I am trying to teach this to my self...
Thanks OverFlow!!!!
ps. Whats the command on a MacBook Pro to type in Unicode and have the corresponding symbol appear?
This should work for you:
NSString *text = inputView.text; // For example "\u267B"
NSLog(@"%@", text);
// Output: \u267B
NSData *d = [text dataUsingEncoding:NSASCIIStringEncoding];
NSString *converted = [[NSString alloc] initWithData:d encoding:NSNonLossyASCIIStringEncoding];
NSLog (@"%@", converted);
// Output: ♻
outputView.text = converted;
It uses the fact that NSNonLossyASCIIStringEncoding
decodes \uNNNN to the corresponding Unicode character.
To convert in the other direction (from "♻" to "\u267B"), you just have to
exchange NSASCIIStringEncoding
and NSNonLossyASCIIStringEncoding
in the above code.
UPDATE: As you correctly noticed, the "reverse direction" does not encode characters in the range <= U+00FF. The following code converts these characters as well:
NSString *text = inputView.text; // For example "♻A"
NSLog(@"%@", text);
// Output: ♻A
NSMutableString *converted = [NSMutableString string];
for (NSInteger i = 0; i < [text length]; i++) {
unichar c = [text characterAtIndex:i];
[converted appendFormat:@"\\u%04X", c];
}
NSLog (@"%@", converted);
// Output: \u267B\u0041
outputView.text = converted;
这篇关于从Unichars来回转换显示字符的最佳方法?目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!