A while ago I found this code and I want to understand how it works)
int index = (int)c % 32 +1;
I have used this line successfully to convert letters into numbers - for example a becomes 1 (and so does A) Could someone please explain how this happens (I've looked a bit into base 32 but am no wiser)?Also would there be an easy way to convert the integer back into a letter?
This is all due to how characters (and strings) are actually represented. Every character is encoded using code points, which are just numbers. Many code points make up a code page which is essentially a table that maps a number to an actual character.
Ignoring the large code pages that come with Unicode, you can just take a look at ASCII for now, which is the encoding for the first 128 code points. There, you can see that the standard upper case alphabet starts at the number 65, while the lower case alphabet starts at the number 97.
So in your formula, if we assume that c
is always a character from the alphabet, we know that its numerical value is between 65 and 90, or between 97 and 122. So taking the character 'A'
or 'a'
, we have a value of 65 or 97 respectively.
All that’s left is the coincidence that the upper case and lower case alphabet start at a difference of 32, and that 65 modulo 32 is 1. This makes (int)c % 32
give you the index of the character in the alphabet starting at 1.