views:

46

answers:

2

Hi,

I want to make a C char array from NSString object in Obj-C.

My string is:

NSString *string = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];

Can someone send me a sample of a working code?

Thanks in advance,

Sagiftw

+3  A: 

You can use the UTF8String method:

const char *str = [string UTF8String];
Jacob Relkin
+1, but I don't want the UTF8String encoding, I lose the Hebrew characters using it!
If you don't want UTF-8 Encoding, use -cStringUsingEncoding: and pass the appropriate encoding.
itaiferber
You won't lose the Hebrew characters by using UTF8String. UTF-8 is capable of representing any Unicode character. If your Hebrew characters are disappearing, the problem is not here.
JeremyP
+1  A: 
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

void *bytes = [data bytes];

(Credit: http://stackoverflow.com/questions/2591052/nsstring-unicode-to-ascii-equivalent)

Or as one line:

void *bytes = [[string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] bytes];
Frank Schmitt
thanks... +1 dd
@user318205: If you didn't like the other answer because it loses Hebrew characters, you definitely won't like this one since you'll just get an array of ASCII characters. There's no Hebrew in ASCII.
JeremyP