字符串转换为HashKey的一种算法

来源:互联网 发布:mac微信截屏快捷键 编辑:程序博客网 时间:2024/06/04 20:00
        public int GetHashValue(string strInput)
        {
            int result = 0 ;
            uint hash = 0;
            int i = 0;
            int sum = 0;
            for (i = 0; i < (strInput.Length - 3); i = i + 4)
            {
                int a = strInput[i];
                a = (a << 8) + strInput[i + 1];
                a = (a << 8) + strInput[i + 2];
                a = (a << 8) + strInput[i + 3];
                sum += a;
            }


            for (; i < strInput.Length; i ++ )
            {
                int a = strInput[i];
                sum += a;
            }
            result = sum % 65535;
            return result;

        }

其中:65535是可以调整的

原创粉丝点击