How to deal with character Encoding in Obj-C?
I'm a new to Obj-C (my experience is in Java and a little C)
I have this project these days, wh开发者_开发问答ich 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 :)
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
}
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"];
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?
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
精彩评论