开发者

Make C char array from NSString object in Obj-C

开发者 https://www.devze.com 2023-01-14 02:56 出处:网络
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];

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];
0

精彩评论

暂无评论...
验证码 换一张
取 消