Image

Imagestesla wrote in Imagecpp

Converting a string to a natural number.

So I'm writing this simple hash for my LJ client, and I find myself needing to convert hash keys into numbers (so that I can go on to hash them). I orignally was going to convert it into a radix-128 integer, but then I realized that would get out of hand for strings bigger than about 10 characters. I ended up going with something like this:
long convertkey(char *key) { //takes string and converts to int for hashing
  int i, j, n, s;
  long ret;
  n = strlen(key);
  s = n > 56 ? n % 56 : 1; //we want to make sure this'll fit into a 64 bit int
  //figure out a better way to do this...
  for(i=0, j=0; i < n; i += s, j++) {
    ret +=  key[i] << j;
  }
  return ret;
}

Does anybody have any suggestions on a better way to do this (this way is likely to cause collisions).