Thread: Hash function in C, need to port it to C++

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Hash function in C, need to port it to C++

    Okay... so i was looking into EternallyConfuzzled's website. The hash part.

    I think it's written for C, but im trying to implement this stuff in C++.

    How would i convert this code?
    Code:
    unsigned djb_hash ( void *key, int len )
     2...{
     3  unsigned char *p = key;
     4  unsigned h = 0;
     5  int i;
     6
     7  for ( i = 0; i < len; i++ )
     8    h = 33 * h + p[i];
     9
    10  return h;
    11}
    Thanks!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    to me it looks like that code should work in both c and c++ (this one djb_hash function at least)

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Junior89 View Post
    How would i convert this code?
    Leave it as-is?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    How should i call it then? Cause i'm obviously not doing it right if i'm getting errors =P
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Junior89 View Post
    How should i call it then? Cause i'm obviously not doing it right if i'm getting errors =P
    Shall we play 20 questions, or will you post your code?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    okay

    Code:
    #include <iostream>
    
    using namespace std;
    
    unsigned hash(void *key, int len);
    
    int main()
    {
    	string key;
    	cout<<"String: ";
    	cin>>key;
    	cin.ignore();
    	int len = strlen(key);
    	cout<<"Hashed: "<<hash(*key, len);
    	cin.ignore();
    	return 0;
    }
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And the rest of it?

    [edit][Your computer doesn't read your mind any more that I do. If you have code to present to it, present it to the compler.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Code:
    unsigned hash(void *key, int len)
    {
    	unsigned char *p = key;
    
    	unsigned h=0;
    
    	int i;
    
    	for(i=0; i<len; i++)
    	{
    		h=33*h+p[i];
    	}
    
    	return h;
    }
    Same as before
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Code:
    #include <iostream>
    
    using namespace std;
    
    unsigned hash(void *key, int len);
    
    int main()
    {
    	char key[20];
    	cout<<"String: ";
    	cin.getline(key, 20);
    	int len = 20;
    	cout<<"Hashed: "<<hash(key, len);
    	cin.ignore();
    	return 0;
    }
    
    unsigned hash(void *key, int len)
    {
    	unsigned char *p = key;
    
    	unsigned h=0;
    
    	int i;
    
    	for(i=0; i<len; i++)
    	{
    		h=33*h+p[i];
    	}
    
    	return h;
    }
    The whole thing so there's no confusion

    Here's the error:
    c:\documents and settings\doug.laptop.000\my documents\visual studio 2005\projects\hash test 1\hash test 1\main.cpp(20) : error C2440: 'initializing' : cannot convert from 'void *' to 'unsigned char *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Thanks! =)
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cout<<"Hashed: "<<hash(*key, len);
    I presume this rather dubious way of converting a C++ String into a C-string then.

    Maybe
    cout<<"Hashed: "<<hash(key.c_str(), len);

    While you're at it, find the correct method for doing this
    int len = strlen(key);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    unsigned char *p = (unsigned char *)key;

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Void pointers don't look very good in C++ programs.

    Just change the prototype to accept the correct pointer type:
    Code:
    unsigned hash(const unsigned char *key, int len)
    {
    	unsigned h=0;
    
    	int i;
    
    	for(i=0; i<len; i++)
    	{
    		h=33*h+key[i];
    	}
    
    	return h;
    }
    And if you want to use it with a std::string, use the c_str() method.

    Edit: more correctly: do the conversion to unsigned where you need the value:
    Code:
    unsigned hash(const char *key, int len)
    {
    	unsigned h=0;
    
    	int i;
    
    	for(i=0; i<len; i++)
    	{
    		h=33*h+(unsigned char)key[i];
    	}
    
    	return h;
    }
    Last edited by anon; 04-05-2007 at 01:18 AM.

  13. #13
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Code:
    #include <iostream>
    
    using namespace std;
    
    unsigned hash(const char *key, int len);
    
    int main()
    {
    	string key;
    	int len;
    	cout<<"Key: ";
    	cin>>key;
    	cin.ignore();
    
    	len = key.length();
    
    	unsigned disp = hash((unsigned char *)key, len);
    	
    	cout<<"Hash: "<<disp;
    	cin.ignore();
    
    	return 0;
    }
    
    unsigned hash(const char *key, int len)
    {
    	unsigned h;
    	unsigned char *p = (unsigned char *)key;
    
    	int i;
    
    	for(i=0; i<len; i++)
    	{
    		h = 33 * h + p[i];
    	}
    	return h;
    }
    Someone Please point out what i'm doing wrong here. I'm very confused. =(
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  14. #14
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    Maybe try passing the address of the first element of the string as your character pointer. It worked for me... I got a compile from your code.
    Code:
    unsigned disp = hash(&key[0], len);
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    unsigned disp = hash((unsigned char *)key, len);
    The red part doesn't give you a pointer to the char array. As key is a std::string, use the string method that returns the pointer: c_str().

    (A pointer to a std::string object != a pointer to the char array that the std::string object holds.)

    Code:
    unsigned disp = hash(key.c_str(), key.size());

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Hash function
    By g_p in forum C Programming
    Replies: 1
    Last Post: 05-29-2007, 05:49 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM