tags:

views:

86

answers:

2

Hi, I am programming with objective c and I want to get character's alphabetical order. For example: A, a - 1, B, b - 2, ... Z, z - 26

Is there a function in Objective C that enables me to achieve this goal?

Thank you in advance, Ilya.

+3  A: 

Well if you can guarantee that the character set is ASCII, then you can just subtract the char from 'A' and then add 1 (only add one if you want to start counting at 1 instead of 0).

Ex:

char ch = 'E';
int num = toupper(ch) - 'A' + 1; // to upper since 'A' and 'a' are not the same character.
// num now is 5
Evan Teran
You need to add 1.
dirkgently
yea, i just noticed that, done.
Evan Teran
+2  A: 

Just keep the lower five bits of the character code:

int n = ch & 0x1F;

The character code of 'A' is 0x41 and 'a' is 0x61, so after stripping off the top three bits they are both 0x01. (It works the same for the rest of the characters too, of course.)

Guffa
clever, not a bad idea.
Evan Teran