如何强制执行.NET字符串GetHash code 32位() [英] How to enforce 32bit of .net String GetHashCode()

查看:116
本文介绍了如何强制执行.NET字符串GetHash code 32位()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/6114772/gethash$c$c-gives-different-results-on-different-servers">GetHash$c$c()给不同的服务器上不同的结果?

我使用的字符串GetHash code()在我的.NET 4.0程序并存储在数据库中的值。问题是,当我运行在不同的安装方案,64和32,.NET中使用不同的哈希算法的字符串散列。所以,我不会得到比赛的所有时间。因此,没有人知道我可以只计算字符串即使是在64位版本散列32位。

I used string GetHashCode() in my .net 4.0 program and store the value in the DB. The problem is that when I run the program on different setup, 64 vs 32, .net use different hashing algorithm for string hashing. So I won’t get the match all the time. So does anyone know how can I just compute the 32bit of string hashing even on a 64bit build.

推荐答案

简单的答案就是那样做。该值被指定为平台相关的,以及确切的行为是不符合的GetHash code合同之外定义。它不能保证留跨越.NET的新版本,新的架构,甚至补丁一样。用一个实际的哈希算法(视实际需求),其行为定义,如SHA-1,CRC,还是其他什么东西。

The simple answer is to not do that. The values are specified as platform-dependent, and the exact behavior is not defined outside of satisfying the contract of GetHashCode. It's not guaranteed to stay the same across new releases of .NET, new architectures, or even patches. Use an actual hash algorithm whose behavior is defined, such as SHA-1, CRC, or something else (depending on the actual requirements).

这篇关于如何强制执行.NET字符串GetHash code 32位()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆