0

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

Sagiftw
  • 1,658
  • 4
  • 21
  • 25

2 Answers2

7

You can use the UTF8String method:

const char *str = [string UTF8String];
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
1
NSData *data = [string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

void *bytes = [data bytes];

(Credit: NSString - Unicode to ASCII equivalent)

Or as one line:

void *bytes = [[string dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] bytes];
Community
  • 1
  • 1
Frank Schmitt
  • 25,648
  • 10
  • 58
  • 70
  • 3
    @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 Sep 01 '10 at 10:30