-1

On a 64-bit .NET 4.5 platform, is there anyway to compute what would be the results of a string's GetHashCode() method on a 32-bit .NET 4.5 platform?

It is obvious this is not a good idea. That has been asked and explained here and here. Only an answer that says how to do it will be accepted.

Community
  • 1
  • 1
Joshcodes
  • 8,513
  • 5
  • 40
  • 47
  • 1
    Why do you want to know how to do it? `GetHashCode()` is only deterministic over one run of the program (in a single app domain). I don't see how your question is any different than the two you linked, the answer is "You can't". – Scott Chamberlain Mar 31 '14 at 23:24
  • The database the application is running over requires it. It was a bad design decision by the original developer. Knowing the answer to this question will be the easiest way to fix it. – Joshcodes Mar 31 '14 at 23:28
  • Please do not edit in answers to the question. This question is getting votes to reopen. I'm looking to vote that way myself, perhaps. – Andrew Barber Apr 01 '14 at 15:35
  • 1
    After reading the question this was marked duplicate of, I'm having a difficult time seeing how the accepted answer there does not answer this question, with simply the additional information that there is no difference based on whether it's running 64/32 bit. – Andrew Barber Apr 01 '14 at 15:37
  • Agreed, one of the "duplication question"'s answers does answer this question. However, it is a different question and the proof is in the fact that there are potential answers to this question that do not answer the other question and vice-versa. Imagine there was a set of build flags that would cause 64-bit to function like 32 bit. That would answer this questions but not the other one. An answer for how GetHashCode functions in .NET 5 or 7 would answer the other question but not answer this question. – Joshcodes Apr 01 '14 at 15:55

1 Answers1

3

GetHashCode() is not 32 bit nor 64 bit specific so calling it on a 32 bit system is the same as calling it on a 64 bit system.

It appears you are trying to reuse a hashcode for some kind of serialization purposes. You can't reliably do that. If it makes it easier, think of string's GetHashCode() function as the following.

static Dictionary <string, int> HashCodes = new Dictionary<string, int>();
static Random Rand = new Random();

static int PsudoGetHashCode(string stringInQuestion)
{
    lock(HashCodes)
    {
        int result;
        if(!HashCodes.TryGetValue(stringInQuestion, out result)
        {
            result = Rand.Next();
            HashCodes[stringInQuestion] = result;
        }

        return result;
    }
}

You are only guaranteed to get the same GetHashCode() value for the same string per run of the program, if you close the program and re-open it you can very possibly get a new value.

Your only solution that will give you reliable results is fix the design of your database to not store the built in GetHashCode() from .NET

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431