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

0

2 Answers 2

7

You can use the UTF8String method:

const char *str = [string UTF8String];
Sign up to request clarification or add additional context in comments.

4 Comments

+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.
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.
what if i dont need const and only char*?
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];

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.