如何为字符串生成唯一的哈希代码 [英] how to generate unique Hash Code for string

查看:191
本文介绍了如何为字符串生成唯一的哈希代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于MSDN没有给我们保证哈希码的唯一性,所以

可以任何人建议我如何为

生成一个独特的哈希码相同的字符串总是,并生成不同的-2 Hash Code Different-2

字符串。

解决方案



" Ashish Khandelwal" < AK *************** @ gmail.com写信息

新闻:e5 *************** ******************* @ x29g2000 prg.googlegroups.com ...


由于MSDN没有给出我们保证Hash Code的唯一性,所以

可以任何人建议我如何为

生成一个独特的Hash代码,并且生成不同的Hash代码不同的2

字符串。



MSDN不能保证这一点,因为不可能给这样的

保证。


考虑到散列的大小是一个int,因此只有32位(或者64位上的
64位)位系统)。这意味着字符串的内容必须是挤压的。转换为那些位,这反过来意味着丢失了大量数据。

对于任何大于哈希大小的给定字符串,总的唯一性

无法保证。我从来没有遇到任何GetHashCode()的问题,

但如果你有,请考虑实施你自己的MD5(或者你认为适当的任何东西)。


感谢您的回复。


但如果您有,请考虑实施您自己的MD5(或任何您的感受

合适。)



是的我面临这样的问题,对于exp。我给你的是blair并且

" brainlessness"字符串,这两个字符串都返回相同的

哈希代码使用GetHashCode()方法,但正如你所说,使用MD5,所以可以

你能给我一些相关的输入到MD5就像我可以回复这个的

输出一样,也就像你说的那样或者你感觉什么是适当的b / b。所以这里同样是我的问题是什么应该是

获得独特的哈希码的方法,任何想法...


Ashish Khandelwal< ; AK *************** @ gmail.comwrote:


由于MSDN不保证哈希码的唯一性,所以

可以任何人建议我如何生成一个独特的哈希码,总是和
相同的字符串,并生成不同的-Hash Code Different-2

字符串。



当然,只要你拥有无穷无尽的数字......我的猜测就是

你没有''但是。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet 博客: http://www.msmvps.com/jon.skeet

世界级.NET培训在英国: http://iterativetraining.co.uk


As MSDN is not giving us guarantee upon uniqueness of Hash Code, so
could any one suggest me that how to generate a unique Hash Code for
same string always, and generate different-2 Hash Code Different-2
string.

解决方案


"Ashish Khandelwal" <AK***************@gmail.comwrote in message
news:e5**********************************@x29g2000 prg.googlegroups.com...

As MSDN is not giving us guarantee upon uniqueness of Hash Code, so
could any one suggest me that how to generate a unique Hash Code for
same string always, and generate different-2 Hash Code Different-2
string.

MSDN can''t guarantee this, because it is not possible to give such a
guarantee.

Consider that the size of the hash is an int, and therefore 32 bits only (or
64 bits on a 64-bit system). This means that the contents of a string must
be "squeezed" into those bits, which in turn means a lot of data is lost.
For any given string bigger than the size of the hash, total uniqueness
cannot be guaranteed. I have never run into any problems with GetHashCode(),
but if you have, consider implementing your own MD5 (or whatever you feel
appropriate.)


Thanks for reply.

but if you have, consider implementing your own MD5 (or whatever you feel
appropriate.)

Yes i am facing such problem, for exp. i am giving you "blair" and
"brainlessness" strings, these both strings are returning the same
Hash Code using GetHashCode() method, but as you said, use MD5, so can
you please give me some input related to MD5 like can i reply on the
output of this, and also as you were saying "or whatever you feel
appropriate" so here just same was my question that what should be the
way to get unique Hash Code, any idea...


Ashish Khandelwal <AK***************@gmail.comwrote:

As MSDN is not giving us guarantee upon uniqueness of Hash Code, so
could any one suggest me that how to generate a unique Hash Code for
same string always, and generate different-2 Hash Code Different-2
string.

Sure, so long as you''ve got an infinite range of numbers... My guess is
that you haven''t though.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


这篇关于如何为字符串生成唯一的哈希代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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