How to create simple short hash value? C#

17,046

Solution 1

Use String.GetHashCode Method

 public static void Main() 
    {
        DisplayHashCode( "Abcdei" );
    }

static void DisplayHashCode( String Operand )
{
    int     HashCode = Operand.GetHashCode( );
    Console.WriteLine("The hash code for \"{0}\" is: 0x{1:X8}, {1}",
                      Operand, HashCode );
}

Solution 2

Use GetHashCode();

public string HashThis(string value)
{
    return hashResult = value.GetHashCode();
}

Solution 3

No you cannot use String.GetHashCode() because this does not guarantee to return the same hash on the same string every time.

Share:
17,046
Smit
Author by

Smit

null

Updated on June 15, 2022

Comments

  • Smit
    Smit about 2 years

    How to create simple hash value? For example I have string "TechnologyIsCool" and how to have hash value from this string?

    I want to do some method like:

    public string HashThis(string value)
    {
        string hashResult = string.Empty;
    
        ...
    
        return hashResult;
    }
    

    and call this method like:

    string hash = HashThis("TechnologyIsCool");
    

    and after that have hash like "5qazws".