7

One of my clients had an app crash and i traced it due to this bug/feature i can't really explain.
The WindowsIdentity.GetCurrent().Name.GetHashCode() returns this string: - ?2097695743
Yes, that a minus, a space, a question mark and then the actual hash numbers.

This is the code of a simple console app that show the weird behaviour.

static void Main(string[] args)
{
    Console.WriteLine("From String: string name = WindowsIdentity.GetCurrent().Name");            
    string name = WindowsIdentity.GetCurrent().Name;
    Console.WriteLine("name:                            " + name);
    Console.WriteLine("name.GetHashCode().GetType():    " + name.GetHashCode().GetType());
    Console.WriteLine("name.GetHashCode():              " + name.GetHashCode());
    Console.WriteLine("name.GetHashCode().ToString():   " + name.GetHashCode().ToString());
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine("Direct");
    Console.WriteLine("WindowsIdentity.GetCurrent().Name:                           " + WindowsIdentity.GetCurrent().Name);
    Console.WriteLine("WindowsIdentity.GetCurrent().Name.GetHashCode().GetType():   " + WindowsIdentity.GetCurrent().Name.GetHashCode().GetType());
    Console.WriteLine("WindowsIdentity.GetCurrent().Name.GetHashCode():             " + WindowsIdentity.GetCurrent().Name.GetHashCode());
    Console.WriteLine("WindowsIdentity.GetCurrent().Name.GetHashCode().ToString():  " + WindowsIdentity.GetCurrent().Name.GetHashCode().ToString());
    Console.WriteLine();
    Console.WriteLine();
    Console.WriteLine("Press Enter to continue");
    Console.ReadLine();
}

This is the text output:

From String: string name = WindowsIdentity.GetCurrent().Name
name:                            COMMARC\tje
name.GetHashCode().GetType():    System.Int32
name.GetHashCode():              - ?2097695743
name.GetHashCode().ToString():   - ?2097695743

Direct
WindowsIdentity.GetCurrent().Name:                           COMMARC\tje
WindowsIdentity.GetCurrent().Name.GetHashCode().GetType():   System.Int32
WindowsIdentity.GetCurrent().Name.GetHashCode():             - ?2097695743
WindowsIdentity.GetCurrent().Name.GetHashCode().ToString():  - ?2097695743


Press Enter to continue

And this is a picture of the same output:

Weird GetHashCode return value

My question is: How on earth is this possible?

UPDATE: the problem was with the funky windows settings for negative numbers.

16
  • 2
    It is not. I suspect the terminal is bonkers. (The only other possibility is that int.ToString() is incorrect, but I doubt that.) Commented Nov 3, 2011 at 21:39
  • 4
    It might be culture clash. What does Name.GetHashCode().ToString(CultureInfo.InvariantCulture) print? Commented Nov 3, 2011 at 21:40
  • 3
    Can you add this to your app? Console.WriteLine("Culture:" + System.Threading.Thread.CurrentThread.CurrentCulture.ToString()); Console.WriteLine("UI Culture:" + System.Threading.Thread.CurrentThread.CurrentUICulture.ToString()); Commented Nov 3, 2011 at 21:48
  • 2
    @MladenPrajic the HashCode returned from GetHashCode has an extremely high chance of collisions - it's not used for identifying data uniquely ONLY to allocate items to buckets in HashSets. You are seeing a crash because you are getting duplicate HashCodes. Generate hash codes for all your users and see if you get duplicates. Commented Nov 3, 2011 at 22:02
  • 3
    @MladenPrajic - ToString() is using the locale of the current machine. Check the regional settings in control panel - there is probably a strange character in the number format. Commented Nov 3, 2011 at 22:11

2 Answers 2

8

If that is the output on the client's computer (but not yours or ours), it's possible the user's machine has specifically configured windows to use "- ?" as the numeric negative symbol. Windows is perfectly willing to let you do that, or any other weird formats.

As a test, I just configured Windows on my machine to use "- ?", and running a simple console app like yours output goofy formatted negative numbers just like your example output. If that's the case on your client's machine, there is nothing wrong with the operation of GetHashCode, it's just an artifact of Windows formatting.

Sign up to request clarification or add additional context in comments.

1 Comment

i'll mark this as answer as it was first. both of you are correct.
7

The problem isn't with GetHashCode(), the problem is with Int32.ToString(). Which is aware of the user's preferences for formatting negative numbers. Control Panel + Region and Language, Formats tab, Additional Settings button. Numbers tab, Negative sign symbol setting. This dialog works a bit differently in earlier versions of Windows, I described the Windows 7 version.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.