char 到 ascii - Objective-C [英] char to ascii - Objective-C
问题描述
我想将char数组转换为ascii,
i want to convert char array to ascii,
我开始尝试使用 NSString、characterAtIndex(由于缺乏操作 unichar 的知识而失败)
i started trying with NSString, characterAtIndex ( failed due to lack of knowledge about manipulating unichar )
创建一个字符数组并使用 stringWithCString 将其传递给 NSString(由于缺乏编码知识而失败)
creating a char array and passing it to a NSString using stringWithCString ( failed due to lack of knowledge about encoding )
现在最后我说服自己使用普通的 char 数组并将其类型转换为 int.
now at the end i am convincing myself using normal char array and type casting it into int.
但我认为这不是一个好习惯.
but my mind says this is not a good practice.
请告诉我是否有另一种 Objective-C 类型的解决方案,或者这是唯一的解决方案.
Please tell me is there another Objective-C type solution, or is this the only solution.
我是初学者,正在研究 GNUStep.
I'm a beginner and am working on GNUStep.
推荐答案
NSString
模拟 Unicode 字符串.要获取 C 字符串编码,请使用 -[NSString cStringUsingEncoding:]
返回一个 const char *
.您必须提供要使用的编码.对于 ASCII,使用 NSASCIIStringEncoding
常量:
NSString
models a Unicode string. To get the C-string encoding, use -[NSString cStringUsingEncoding:]
which returns a const char *
. You'll have to supply the encoding you want to use. For ASCII, use the NSASCIIStringEncoding
constant:
const char *asciiCString = [myString cStringWithEncoding:NSASCIIStringEncoding];
这篇关于char 到 ascii - Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!