1

here i have code for calculate hash value of unsigned char

#include <cstdlib>
#include <iostream>
#include<string.h>
using namespace std;

unsigned oat_hash(unsigned char *key,int len)
{
    unsigned char *p=key;
    unsigned h=0;
    int i;
    for(i=0;i<len;++i){
        h+=p[i];
        h+=(h<<10);
        h^=(h>>6);
    }
    h+=(h<<3);
    h^=(h>>11);
    h+=(h<<15);

    return h;
}

using namespace std;

int main(int argc, char *argv[])
{
    unsigned char mystring[]="123456789abcdef";
    unsigned char *key=&mystring[0];
    int n=sizeof(mystring)/sizeof(mystring[0]);//length of mystring
    cout<<oat_hash(key,n)<<endl;
    //system("PAUSE");
    //return EXIT_SUCCESS;
    return 0;
}

name of this hash function is so called One-at-a-Time hash(by Bob Jenkins) i have one question is this little part of code correct?

int n=sizeof(mystring)/sizeof(mystring[0]);//length of mystring

because mysting has not built-in function length,i used this

Bart
  • 19,692
  • 7
  • 68
  • 77

3 Answers3

3

Under the circumstances, yes -- but it's pretty fragile. For example, if you changed your definition from:

unsigned char mystring[]="123456789abcdef";

To:

unsigned char *mystring="123456789abcdef";

Your method of finding the length would produce completely incorrect results. Also note that since your string is made up of chars, the /sizeof(mystring[0]) isn't really necessary either -- sizof(char) == 1 (and the same for signed char or unsigned char).

You normally want to use strlen instead.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Since it's `unsigned char[]`, I believe you would need to convert `unsigned char*` to `const char*` for `std::strlen` (also, the count would be 15 instead of 16). – Jesse Good Mar 16 '12 at 06:08
  • @Jesse: To fully understand the excellent advice in Jeffry's answer(+1) You might want to have a look at this [C++Faq](http://stackoverflow.com/questions/9460260/what-is-the-difference-between-char-a-string-and-char-p-string) entry. – Alok Save Mar 16 '12 at 07:23
  • @Als: I'm not sure what the C++faq has to do with my comment. – Jesse Good Mar 16 '12 at 08:20
  • Oh my, sorry that was meant for OP @dato – Alok Save Mar 16 '12 at 08:21
1

Yeah, your code is correct. You may want to compare against the data-type though:

int n=sizeof(mystring) / sizeof(char); //length of mystring

Note that this only works if the string isn't dynamic. Otherwise use strlen for c-style strings.

I must say, however, C++'s std::string does have a length method, and is much easier to use in the majority of cases - especially when using them with the STL.

Also, boost can do C++ string hashes

Community
  • 1
  • 1
Seb Holzapfel
  • 3,793
  • 1
  • 19
  • 22
0

Yes I feel that the code will work fine. But ensure that if you pass an array of a string through a method it would not give you the desired result since passing array in functions implicitly passed by a pointer. That time your code can provide a disaster. Other wise it is fine. The other way you can find the length of a string array is like:

int len = 0;
int iCount = 0;
while (mystring[iCount].empty() != true)
{
      iCount++;
      len++;
}

Then use len as length of the String array

Hope this will help.

Anirban Roy
  • 111
  • 1
  • 1
  • 9