Objective-C NSString与char *互相转换

Objective-C NSString与char *互相转换,第1张

Objective-C NSString与char *互相转换 1,NSString 转 char *
--UTF8String
const char *cString = [string UTF8String];

--cStringUsingEncoding:
const char *cString = [string cStringUsingEncoding:NSUTF8StringEncoding];

--getCString:maxLength:encoding:
char cStringPtr[string.length * 3 + 1];
memset(cStringPtr, 0, sizeof(cStringPtr));
[string getCString:cStringPtr maxLength:sizeof(cStringPtr) encoding:NSUTF8StringEncoding];
2,char* 转 NSString
----initWithUTF8String:

NSString *string = [[NSString alloc] initWithUTF8String:cString];

----initWithCString:encoding:
NSString *string = [[NSString alloc] initWithCString:cString encoding:NSUTF8StringEncoding];

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5651114.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存