I want to make a C char array from NSString object in Obj-C.
My string is:
NSString *string 开发者_StackOverflow社区= [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
Can someone send me a sample of a working code?
Thanks in advance,
Sagiftw
You can use the UTF8String
method:
const char *str = [string UTF8String];
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];
精彩评论