经典字符串中出现的hash函数 (转载自http://www.ccw.com.cn/htm/app/aprog/01_8_22_3.asp) 1. PHP中出现的字符串Hash函数 static unsigned long str_hash_function (char *arKey , unsigned int nKeyLength) { int h=0; int g; char *arEnd=arKey+nKeyLength; while (arKey<arEnd) { h=(h<<4)+*arKey++; if ((g=(h&0xF0000000))) { h=h^(g>>24); h=h^g; } } return h; } 2. OpenSSL中出现的字符串Hash函数 unsigned long str_hash_function (char *str) { int i , l; unsigned long ret=0; unsigned short *s; if (str==NULL) return (0); l=(strlen(str)+1)/2; s=(unsigned short *) str; for (i=0;i<l;i++) { ret^=(s[i]<<(i&oxof)); return ret; } unsinged long str_hash_function2 (const char *str) { unsigned long ret=0; long n; unsigned long v; int r; if ((c==NULL)||(*c==’\0’)) return ret; n=0x100; while (*c) { v=n| (*c); n+=0x100; r=(int )((v>>2)^v)&0x0f; ret=(ret<>(32-r)); ret&=0xFFFFFFFFL; retu^=v*v; c++; } return ((ret>>16)^ret); } 3. MySql中出现的字符串Hash函数(完全看不懂就不写了) 4. 很简单又好用的一个字符串的Hash函数 int hash_function () { int h=0; int i; for (i=0;b[i];i++) h=5*h+b[i]; return h; }

评论