Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the int ASCII value of a character in Cocoa?

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?

like image 900
Cashew Avatar asked Jul 08 '10 14:07

Cashew


2 Answers

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.

like image 146
Matthew Flaschen Avatar answered Sep 17 '22 20:09

Matthew Flaschen


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;
}
like image 33
aitorkun Avatar answered Sep 16 '22 20:09

aitorkun