从Unichars来回转换显示字符的最佳方法?目标C [英] Best way to convert Back and forth from Unichars to display characters? Objective C

查看:240
本文介绍了从Unichars来回转换显示字符的最佳方法?目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试着了解Unichars。 Unichars来自Ascii,似乎是一个更高级和更复杂的字符代码库。现在,我正在构建一个简单的应用程序,该应用程序将接受将表示Unicode的字符串\\\ ####,这将导致我的第一个问题。什么是格式化Unicode的最佳方法,搜索了这么久以后,我觉得我已经看到了很多方法来做到这一点。或者,也许我还没有最好的概念。



让我们拿回收符号为例。 U + 267B



我想让我的程序把它转换成实际的符号,并显示出来吗?

然后id喜欢阅读任何符号,并将其转换回Unicode?

我期待这是非常简单的,但我想教我这个自我。 ..

谢谢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屋!

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