Reputation: 1474
How do I get the ASCII value as an int of a character in Cocoa? I found an answer for doing this in python, but I need to know how to do this in Cocoa. ( I am still a noob in Cocoa).
Python method:
use function ord() like this:
>>> ord('a')
97
and also chr() for the other way around:
>>> chr(97)
'a'
how do I do this in Cocoa?
Upvotes: 6
Views: 4578
Reputation: 33
It has nothing to do with Cocoa, it depends on the language, simply in C or C++ make a cast to int to the char :)
C++:
#include <iostream>
int main()
{
int number;
char foo = 'a';
number = (int)foo;
std::cout << number << std::endl;
return 0;
}
Upvotes: 1
Reputation: 284786
Character constants are already integers:
int aVal = 'a'; // a is 97, in the very likely event you're using ASCII or UTF-8.
This really doesn't have anything to do with Cocoa, which is a library. It's part of C, so it's not specific to Objective-C either.
Upvotes: 12