hash function

Discussion in 'C Programming' started by Lars Tackmann, Dec 19, 2003.

  1. 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 -
     
    Lars Tackmann, Dec 19, 2003
    #1
    1. Advertising

  2. Lars Tackmann

    CBFalconer Guest

    Lars Tackmann wrote:
    >
    > 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:


    hashlib.zip contains some string hashing utility functions,
    together with some URL references to others. See:

    <http://cbfalconer.home.att.net/download/>

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Dec 19, 2003
    #2
    1. Advertising

  3. Lars Tackmann

    osmium Guest

    Lars Tackmann writes:

    > 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.
     
    osmium, Dec 19, 2003
    #3
  4. Lars Tackmann wrote:

    > 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
     
    glen herrmannsfeldt, Dec 20, 2003
    #4
  5. Lars Tackmann

    Paul Hsieh Guest

    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/

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
     
    Paul Hsieh, Dec 21, 2003
    #5
  6. "osmium" <> wrote in
    news:brv2hd$7sc26$-berlin.de on Fri 19 Dec 2003
    09: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.
     
    August Derleth, Dec 22, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,046
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,113
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    789
  4. rp
    Replies:
    1
    Views:
    534
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    624
    David A. Black
    Jul 2, 2008
Loading...

Share This Page