hash function

L

Lars Tackmann

Hi i need a hash function for some large strings (upto 1024 chars in the
array) the following is not very good for these large strings:

int string_hash(int hash_size, const char *key)
{
unsigned long n = 0;

const char *p;
for (p = key; *p != '\0'; p++)
n = ((n << 8) + n) ^ (unsigned long)(*p);
printf("n er: %i", n);
return (n % hash_size);
}

Any ideas -
 
O

osmium

Lars said:
Hi i need a hash function for some large strings (upto 1024 chars in the
array) the following is not very good for these large strings:

int string_hash(int hash_size, const char *key)
{
unsigned long n = 0;

const char *p;
for (p = key; *p != '\0'; p++)
n = ((n << 8) + n) ^ (unsigned long)(*p);
printf("n er: %i", n);
return (n % hash_size);
}

What is it that you don't like about what you already have? I have the
impression that MD5 is the _ne plus ultra_ of hash functions if you aren't
concerned about speed.
 
G

glen herrmannsfeldt

Lars said:
Hi i need a hash function for some large strings (upto 1024 chars in the
array) the following is not very good for these large strings:

int string_hash(int hash_size, const char *key)
{
unsigned long n = 0;

const char *p;
for (p = key; *p != '\0'; p++)
n = ((n << 8) + n) ^ (unsigned long)(*p);
printf("n er: %i", n);
return (n % hash_size);
}

I would use CRC32, and there is already a thread discussing it.

-- glen
 
P

Paul Hsieh

(e-mail address removed) says...
Hi i need a hash function for some large strings (upto 1024 chars in the
array) the following is not very good for these large strings:

int string_hash(int hash_size, const char *key)
{
unsigned long n = 0;

const char *p;
for (p = key; *p != '\0'; p++)
n = ((n << 8) + n) ^ (unsigned long)(*p);
printf("n er: %i", n);
return (n % hash_size);
}

I belive that you will find the state of the art in hash functions here:

http://www.isthe.com/chongo/tech/comp/fnv/
 
A

August Derleth

38:17a:
I have the
impression that MD5 is the _ne plus ultra_ of hash functions if you
aren't concerned about speed.

What about SHA? I think SHA is slowly replacing MD5 in most arenas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top