Reputation: 2950
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.
Upvotes: 0
Views: 4327
Reputation: 107754
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];
Upvotes: 2
Reputation: 12187
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*...
Upvotes: 0