简体   繁体   English

如何为字符串生成唯一的哈希码

[英]How can I generate a unique hashcode for a string

Is there any function, that gives me the same hashcode for the same string? 有没有任何函数,它为同一个字符串提供相同的哈希码?

I'm having trouble when creating 2 different strings (but with the same content), their hashcode is different and therefore is not correctly used in a Dictionary . 我在创建2个不同的字符串(但具有相同的内容)时遇到了麻烦,但它们的哈希码是不同的,因此未在Dictionary正确使用。

I would like to know what GetHashCode() function the Dictionary uses when the key is a string. 我想知道当键是字符串时Dictionary使用的GetHashCode()函数。

I'm building mine like this: 我正在这样建造我的:

public override int GetHashCode()
{
   String str = "Equip" + Equipment.ToString() + "Destiny" + Destiny.ToString();
   return str.GetHashCode();
}

But it's producing different results for every instance that uses this code, despite the content of the string being the same. 但是它为使用此代码的每个实例产生不同的结果,尽管字符串的内容是相同的。

Your title asks for one thing ( unique hash codes) your body asks for something different ( consistent hash codes). 你的标题要求一件事( 唯一的哈希码)你的身体要求不同的东西( 一致的哈希码)。

You claim: 你声称:

I'm having trouble when creating 2 different strings (but with the same content), their hashcode is different and therefore is not correctly used in a Dictionary. 我在创建2个不同的字符串(但具有相同的内容)时遇到了麻烦,但它们的哈希码是不同的,因此未在字典中正确使用。

If the strings genuinely have the same content, that simply won't occur. 如果字符串真的具有相同的内容,那就不会发生。 Your diagnostics are wrong somehow. 你的诊断错误了。 Check for non-printable characters in your strings, eg trailing Unicode "null" characters: 检查字符串中的不可打印字符,例如尾随Unicode“null”字符:

string text1 = "Hello";
string text2 = "Hello\0";

Here text1 and text2 may print the same way in some contexts, but I'd hope they'd have different hash codes. text1text2在某些上下文中可能会以相同的方式打印,但我希望它们有不同的哈希码。

Note that hash codes are not guaranteed to be unique and can't be... there are only 2 32 possible hash codes returned from GetHashCode , but more than 2 32 possible different strings. 请注意,哈希码不能保证是唯一的, 并且不能 ......从GetHashCode返回的只有2 32个可能的哈希码,但是超过2 32个可能的不同字符串。

Also note that the same content is not guaranteed to produce the same hash code on different runs, even of the same executable - you should not be persisting a hash code anywhere. 另请注意,相同的内容不能保证在不同的运行中生成相同的哈希代码,即使是相同的可执行文件 - 您不应该在任何地方持久化哈希代码。 For example, I believe the 32-bit .NET 4 and 64-bit .NET 4 CLRs produce different hash codes for strings. 例如,我相信32位.NET 4和64位.NET 4 CLR为字符串生成不同的哈希码。 However, your claim that the values aren't being stored correctly in a Dictionary suggests that this is within a single process - where everything should be consistent. 但是,您声称值未正确存储在Dictionary表明这是在一个过程中 - 一切都应该是一致的。

As noted in comments, it's entirely possible that you're overriding Equals incorrectly. 正如评论中所指出的那样,你完全有可能错误地重写了Equals I'd also suggest that your approach to building a hash code isn't great. 我还建议你构建哈希码的方法不是很好。 We don't know what the types of Equipment and Destiny are, but I'd suggest you should use something like: 我们不知道什么类型的EquipmentDestiny ,但我建议你应该使用类似的东西:

public override int GetHashCode()
{
    int hash = 23;
    hash = hash * 31 + Equipment.GetHashCode();
    hash = hash * 31 + Destiny.GetHashCode();
    return hash;
}

That's the approach I usually use for hash codes. 这是我通常用于哈希码的方法。 Equals would then look something like: Equals将看起来像:

public override bool Equals(object other)
{
    // Reference equality check
    if (this == other)
    {
        return true;
    }         
    if (other == null)
    {
        return false;
    }
    // Details of this might change depending on your situation; we'd
    // need more information
    if (other.GetType() != GetType())
    {
        return false;
    }

    // Adjust for your type...
    Foo otherFoo = (Foo) other;

    // You may want to change the equality used here based on the
    // types of Equipment and Destiny
    return this.Destiny == otherFoo.Destiny &&
           this.Equipment == otherFoo.Equipment;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM