views:

78

answers:

4

Hi, 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 :)

+1  A: 
  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?

zoul
Unichar does not have to work out of the box, see Jeremy's answer.
zoul
+1  A: 

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
}
nevan
Thank you so much nevan :).. this is really helpful and it worked successfully :D
+1  A: 

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"];
JeremyP
thank you Jeremy .. yeah it worked, but what about using NSString as nevan suggested ? it's so good so far :)
There's several ways to get that character into an NSString. I'll edit the answer to give a couple of examples...
JeremyP
A: 

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
UncleMiF
Thank you UncleMiF :D .. could you please tell me why I'll use NSData instead of NSString ?
Since encoding often means work with raw data.
UncleMiF
Raw data is bytes level, so you don't need to worry about character set (except up level presentation like NSUTF8StringEncoding etc)
UncleMiF
And, one more comment - I don't recommend you drop NSString, I just suggest you use NSData received from NSString, when you work with encoding. Then restore NSString from NSData when you work with decoding. Useful crypto algorithms are not simple XOR-like, so when you wish use OpenSSL layer it's more important to have raw bytes (NSData takes care about).
UncleMiF
thanks, I really appreciate your help :)