C++ base-26 string hashing formula

I have a data structure contains a 4-letter string as a key (char key[5]) with all the characters in upper case. I'm trying to write a formula to calculate a base-26 hash index for a key in order to place the structure in a hash table that is TABLESIZE in length. The main thing I wasn't sure about what having enough key[] to index all my data being that it is key[5]. Here is the formula I have written so far:

 
long index = ((key[0]-'A')*pow(26,3)) + ((key[1]-'A')*pow(26,2)) + ((key[2]-'A')*26) + ((key[3]-'A')) % TABLESIZE;


Thank you for any advice to improve my formula.
Your formula is correct though you can write it easier:

 
((((key[0] - 'A') * 26 + (key[1] -'A')) * 26 + (key[2] - 'A')) * 26 + (key[3] - 'A')) % TABLESIZE;


As you see, you can even put it into the loop.

Moreover, since it is hash, you really need not subtract 'A' from letters.

Since the fifth char is always 0 you need not add it to hash, though you can if you really want. This will not change the matter. :)

So summing up you'll have:
1
2
3
4
5
int hash = 0;
for (int i = 0; i < 4; i++) {
    hash = hash * 29 + key[i];
}
hash %= TABLESIZE;


I use 29 instead 26 since obviously for hashes it is always better to choose multipliers which are coprime with both table size and MAX_INTEGER.
Last edited on
Topic archived. No new replies allowed.