C # string.GetHashCode () returns non int result

One of my clients had an application crash, and I traced it due to this error / function, which I cannot explain.
WindowsIdentity.GetCurrent (). Name.GetHashCode () returns this string: -? 2097695743
Yes, that’s minus, space, question mark, and then the actual hash numbers.

This is the code for a simple console application displaying strange behavior.

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 the image of the same output:

Weird GetHashCode return value

My question is: how is this possible?

UPDATE: The problem was setting up funky windows for negative numbers.

+7
source share
2 answers

If this is the result on the client computer (but not yours or ours), the user computer may have specially configured windows to use "-?" as a numeric negative character. Windows is ready to let you do this or any other weird formats.

As a test, I just configured Windows on my machine to use β€œ-?", And I run a simple console application like your outputs formatted with negative numbers, just like your output example. If this is on your client computer, there is nothing wrong with GetHashCode, it's just an artifact of Windows formatting.

+8
source

The problem is not GetHashCode (), the problem is Int32.ToString (). What is known about user preferences for formatting negative numbers. Control panel + Region and language, tab "Formats", button "Advanced settings". Numbers tab, Negative sign. This dialog works differently in earlier versions of Windows; I described the version of Windows 7.

+7
source

All Articles