Hash function
Jump to navigation
Jump to search
A hash function is a function that returns the hash value of a string. Hashing means to convert a string into a number.
Process
This is an example hashing process:
idx(si) return si(Let's assume that a~z is 1~26) hash[i]=(hash[i-1]*idx(si)) % mod p and mod are both prime numbers, where p<mod Assume that p=13, mod=101 Now let's hash the string "abc". We need an array to store the hash process (or a variable might be enough.) hash[0]=1; hash[1]=(hash[0]*13+2)%101=15; hash[2]=(hash[1]*13+3)%101=97; So, "abc"'s hash value is 97. Sometimes, the hashing process will return the same value from different strings. Refer to https://www.planetmath.org/goodhashtableprimes for more information.
C example
unsigned hash(const char a[static 1]) { unsigned h = 0; for (size_t i = 0; a[i] != '\0'; i++) h = 65 * h + a[i]; return h; }