But in some occasion, what you need is not really a GUID, but a random string that is "unique enough" to be used by your system. And you might want to store it as a string in your database (for whatever reason).
In C#, you can do this by using the GUID hash code. Here is the function that returns a "unique enough" string with a desired length:
/// <summary>
/// Gets a unqiue key based on GUID string hash code.
/// </summary>
/// <param name="length">The length of the key returned.</param>
/// <returns>Unique key returned.</returns>
public static string GetUniqueKey(int length)
{
string guidResult = string.Empty;
while (guidResult.Length < length)
{
// Get the GUID.
guidResult += Guid.NewGuid().ToString().GetHashCode().ToString("x");
}
// Make sure length is valid.
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
// Return the first length bytes.
return guidResult.Substring(0, length);
}
If you find this post helpful, would you buy me a coffee?
this is predictable... so use with caution.. meaning, don't use it for any kind of security. use the random hash generation if you want a secure key.
ReplyDeletethere is no difference just use Random.Next(length).ToString('x')
ReplyDeletevidio ayam sabung ayam bali bangkok youtube
ReplyDelete