开发者

char to ascii - Objective-C

i want to convert char array to ascii,

i started trying 开发者_开发问答with NSString, characterAtIndex ( failed due to lack of knowledge about manipulating unichar )

creating a char array and passing it to a NSString using stringWithCString ( failed due to lack of knowledge about encoding )

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.

Please tell me is there another Objective-C type solution, or is this the only solution.

I'm a beginner and am working on GNUStep.


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


You either want to work with a char-array, which is probably a C-string, and already in ASCII, OR You can use an NSString, which is in unicode, and is an opaque data type.

If you're using NSString, use the appropriate NSString functions, and you don't ever really need to convert to a char*...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜