Obj-C中如何处理字符编码?

发布于 2024-09-29 21:37:52 字数 281 浏览 6 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

筑梦 2024-10-06 21:37:52

如果你想在 Objective-C 中处理 Unicode,你应该使用 NSString 而不是 char 类型。 NSString 被设置为处理 Unicode。

使用 characterAtIndex循环遍历字符串

for (characterIndex = 0; characterIndex < [myString length]; characterIndex++)
{
    unichar testCharacter = [myString characterAtIndex:characterIndex];
    // do stuff
}

If you want to deal with Unicode in Objective-C, you should use NSString instead of char types. NSString is set up to deal with Unicode.

Use characterAtIndex to loop through the string

for (characterIndex = 0; characterIndex < [myString length]; characterIndex++)
{
    unichar testCharacter = [myString characterAtIndex:characterIndex];
    // do stuff
}
〆凄凉。 2024-10-06 21:37:52

字符查看器告诉我您的字符是 unicode 编号 0x628。它太大了,无法存储在只有 8 位的单个字符中。好消息是它适合 unichar 所以:

unichar c = 'ب';

可能会起作用。但编译器不保证能够处理有限字符集之外的字符。为了安全起见,您可能希望显式使用 UTF-16 编码(这是 NSStrings 内部使用的编码。所以:

unichar c = 0x628; // Arabic Beh (UTF-16)

或者如果您更喜欢 UTF-8,则该 unicode 数字的 UTF-8 编码是 D8 A8:

char c[2] = { 0xD8, 0xA8 };  // Arabic Beh (UTF-8)

编辑:

一些获取方法将字符转换为 NSString:

使用 -stringWithFormat:

NSString* foo = [NSString stringWithFormat: @"beh %C", (unichar) 0x628];

或者

NSString* foo = [NSString stringWithUTF8String: "beh \xD8\xAB"];

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:

unichar c = 'ب';

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:

unichar c = 0x628; // Arabic Beh (UTF-16)

Or if you prefer UTF-8, the UTF-8 encoding for that unicode number is D8 A8:

char c[2] = { 0xD8, 0xA8 };  // Arabic Beh (UTF-8)

Edit:

Some ways to get the character into an NSString:

Use -stringWithFormat:

NSString* foo = [NSString stringWithFormat: @"beh %C", (unichar) 0x628];

Or

NSString* foo = [NSString stringWithUTF8String: "beh \xD8\xAB"];
夏有森光若流苏 2024-10-06 21:37:52
  1. 您尝试过unichar吗?无论编码如何,简单的 char 都无法工作,它太小了。

  2. 你真的需要使用单个角色吗? NSString 不行吗?

  3. 那是什么类型的加密?难道你不能对字节流进行加密,不管它们的含义是什么吗?

  1. Did you try unichar? Simple char will not work regardless of the encoding, it’s too small.

  2. Do you really need to work with single characters? NSString will not do?

  3. What kind of encryption is that? Couldn’t you encrypt byte streams regardless of their meaning?

只涨不跌 2024-10-06 21:37:52

我建议你使用 NSData。
所以,你所需要的就是从 NSString 接收 NSData 对象,
然后请求它的字节,对它们进行编码,
写下它们。
下一个。
从该数据加载、解码和构造 NSString。
以下是有用的方法:

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding// for NSString
- (const void *)bytes// for NSData
- (void *)mutableBytes// if you prefer work with NSMutableData constructed from NSData with mutableCopy method
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding// to restore NSString back when decoding

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:

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding// for NSString
- (const void *)bytes// for NSData
- (void *)mutableBytes// if you prefer work with NSMutableData constructed from NSData with mutableCopy method
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding// to restore NSString back when decoding
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文