Obj-C中如何处理字符编码?
我是 Obj-C 的新手(我的经验是 Java 和一点 C),
这些天我有这个项目,这是一个阿拉伯文本加密..我需要读取一个阿拉伯文本文件(逐个字符),但是当我想使用这些字符并将它们存储在变量(char类型)中时我不能..它在这一行给了我这个警告“多字符字符常量”:
char c = 'party'; // 这里我试图将字母“Bah”存储在一个char变量中
我认为这是一个编码问题,但我不知道问题到底是什么,我花了过去2天的时间寻找解决方案,但是找不到一个:(..
提前致谢:)
I'm a new to Obj-C (my experience is in Java and a little C)
I have this project these days, which is An Arabic-Text encryption .. I need to read an arabic text file (character by character), but when I want to use these characters and store them in variables (of type char) I couldn't .. it gives me this warning "Multi-character character constant" on this line :
char c = 'ب'; // here I'm trying to store the letter "Bah" in a char variable
I think it's an encoding problem, but I don't know what exactly the problem is, and I spent the last 2 days looking for a solution, but couldn't find one :( ..
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想在 Objective-C 中处理 Unicode,你应该使用
NSString
而不是char
类型。NSString
被设置为处理 Unicode。使用
characterAtIndex
来循环遍历字符串If you want to deal with Unicode in Objective-C, you should use
NSString
instead ofchar
types.NSString
is set up to deal with Unicode.Use
characterAtIndex
to loop through the string字符查看器告诉我您的字符是 unicode 编号 0x628。它太大了,无法存储在只有 8 位的单个字符中。好消息是它适合 unichar 所以:
可能会起作用。但编译器不保证能够处理有限字符集之外的字符。为了安全起见,您可能希望显式使用 UTF-16 编码(这是 NSStrings 内部使用的编码。所以:
或者如果您更喜欢 UTF-8,则该 unicode 数字的 UTF-8 编码是 D8 A8:
编辑:
一些获取方法将字符转换为 NSString:
使用 -stringWithFormat:
或者
The chracter viewer tells me your character is unicode number 0x628. It's too big to store in a single char which is only 8 bits. The good news is that it will fit in a unichar so:
might work. But the compiler doesn't guarantee to be able to handle characters outside a limited character set. For safety you might want to use the UTF-16 encoding explicitly (this is what NSStrings use internally. So:
Or if you prefer UTF-8, the UTF-8 encoding for that unicode number is D8 A8:
Edit:
Some ways to get the character into an NSString:
Use -stringWithFormat:
Or
您尝试过
unichar
吗?无论编码如何,简单的char
都无法工作,它太小了。你真的需要使用单个角色吗?
NSString
不行吗?那是什么类型的加密?难道你不能对字节流进行加密,不管它们的含义是什么吗?
Did you try
unichar
? Simplechar
will not work regardless of the encoding, it’s too small.Do you really need to work with single characters?
NSString
will not do?What kind of encryption is that? Couldn’t you encrypt byte streams regardless of their meaning?
我建议你使用 NSData。
所以,你所需要的就是从 NSString 接收 NSData 对象,
然后请求它的字节,对它们进行编码,
写下它们。
下一个。
从该数据加载、解码和构造 NSString。
以下是有用的方法:
I suggest you use NSData.
So, all what you need is receive NSData object from NSString,
then request its bytes, encode them,
write them.
Next.
Load, decode, and construct NSString from that data.
Here are useful methods: