Discussion Locked

String hash keys

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Does anybody have an algorithm for producing a numerical hash key from an arbitrary length string? The ideal answer will distribute the keys evenly over a configurable range (e.g. 0 .. 4096) and similar strings will get different keys e.g. "jeremp"and "jeremyq" will get different keys.

10 replies

by marco

You can use the MD5 message digest algorithm. The algorithm takes as input a message of arbitrary length and produces as output a 128-bit message digest of the input. Of couse, similar strings will result in different keys. For details, please readRFC 1321

by jpereira

This is way too complex for what I want to do, which is implement a string indexed hash table.

by shadowtraitor

Yo hope you know C++ cause here is some C++ code for ya.

int Player::Hash(const std::string& name,int tablesize)
{
int hashvalue = 0;
for(int i = 0; i < name.length();i++)
hashvalue += name[i];//hash value summation of ascii value of key
//reduce hash value to be within 0 and the size of the table
return (hashvalue % tablesize);
}

This function takes the string and goes through it character by character converting each to its ASCII value and the sums thesevalues, until the each character is converted. Then using the size of the hash table get the modulus of the hash value this ensures that the hash value is between 0 and the size of the hash table

Hope i helped man..

by jpereira

This answer does not satisfy the evenness criterium. e.g. if I have lots of strings with less than 8 chars, the hash key will always be in the range 0 .. 1016 (for Ascii values)

by shadowtraitor

Yo hope you know C++ cause here is some C++ code for ya.

int Player::Hash(const std::string& name,int tablesize)
{
int hashvalue = 0;
for(int i = 0; i < name.length();i++)
hashvalue += name[i];//hash value summation of ascii value of key
//reduce hash value to be within 0 and the size of the table
return (hashvalue % tablesize);
}

This function takes the string and goes through it character by character converting each to its ASCII value and the sums thesevalues, until the each character is converted. Then using the size of the hash table get the modulus of the hash value this ensures that the hash value is between 0 and the size of the hash table

Hope i helped man..

by ahicks

The best place to look is Knuth's Art of Computer Programming Vol 2 Searching and Sorting. He covers hashing in great detail.
It is important to note that he recommended filling up no more than 80% of the hash space and making the hash space a prime number of records for optimal results.

Generally, hash routines compute a running total of the each character and the product of a prime number. For example, 13,7,5, and 3 for a key that has a max of 4 characters. The modulo arithmetic is applied to the final sum to insure that it falls into the proper range. Using larger numbers for the first letters of the key assures maximum scattering and minimizes the number of collisions (i.e. two keys yielding the same hash value). The prime numberfor the number of records also reduces the possibility of collisions. Your choice of 4096 is not a good size since it is a power of 2.

by jpereira

This doesn't work for arbitrary length keys, although I could just limit the hash to (say) the first 16 characters.

This discussion is closed to new replies.