设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1450|回复: 0
打印 上一主题 下一主题

【转载】.net中string所采用的散列函数

[复制链接]
跳转到指定楼层
1#
发表于 2014-2-23 10:31:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  • using System;  
  • namespace ConsoleApplication8  
  • {  
  •     class Program  
  •     {  
  •         //项目属性对话框->配置属性->生成->允许不安全代码块->设为true  
  •         static public unsafe int DotNetHash(string str)  
  •         {  
  •             fixed (char* charPtr = new String(str.ToCharArray()))  
  •             {  
  •                 int hashCode = (5381 << 16) + 5381;  
  •                 int numeric = hashCode;  
  •                 int* intPtr = (int*)charPtr;  
  •                 for (int i = str.Length; i > 0; i -= 4)  
  •                 {  
  •                     hashCode = ((hashCode << 5) + hashCode + (hashCode >> 27)) ^ intPtr[0];  
  •                     if (i <= 2) break;  
  •                     numeric = ((numeric << 5) + numeric + (numeric >> 27)) ^ intPtr[1];  
  •                     intPtr += 2;  
  •                 }  
  •                 return hashCode + numeric * 1566083941;  
  •             }  
  •         }  
  •         static void Main(string[] args)  
  •         {  
  •             string test = "test it!";  
  •             Console.WriteLine(test.GetHashCode());  
  •             Console.WriteLine(DotNetHash(test));  
  •             Console.ReadKey();  
  •         }  
  •     }  
  • }  





分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2025-2-23 20:00 , Processed in 0.073905 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表