Thursday, September 3, 2009

Calculate GUID from object or string

private Guid CalculateGUID(object m)
{
    ASCIIEncoding enc = new ASCIIEncoding();
    string code = m.GetHashCode( ).ToString();
    string key;
    if (code.Length < 16)
        key = code + new string( '*', 16 - code.Length );
    else if (code.Length == 16)
    {
        key = code;
    }
    else
    {
        key = code.Substring( 0, 16 );
    }

    return new Guid(enc.GetBytes(key)) ;
}