Generate a unique value for a combination of two numbers

后端 未结 9 851
有刺的猬
有刺的猬 2020-12-16 16:09

Consider I\'ve two numbers 1023232 & 44. I want to generate a unique number representing this combination of numbers. How can i generate it?

Requirement

相关标签:
9条回答
  • 2020-12-16 17:02

    If you can represent it as a string, this should work:

    Hash((int1 | int2).ToString());
    

    Like so:

    public static string Hash(string plaintext)
    {
    var hashAlgorithm = new SHA1CryptoServiceProvider();
    var unhashedBuffer = Encoding.Default.GetBytes(plaintext);
    var hashedBuffer = hashAlgorithm.ComputeHash(unhashedBuffer);
    return Convert.ToBase64String(hashedBuffer);
    )
    
    0 讨论(0)
  • 2020-12-16 17:02

    If X & Y are Int add a seperator. Always unique.

    X = 100, Y = 5 => 100.5

    X = 1023232, Y = 44 => 1023232.44

    0 讨论(0)
  • 2020-12-16 17:04

    If you are using ints and don't mind the result being a long, this should work:

    Math.Max(x, y) << 32 | Math.Min(x, y)

    The fact that the numbers are stored in the high and low dwords of the result get you your uniqueness constraint.

    The fact that the higher number is always in the high dword gets you the symmetry you wanted.

    0 讨论(0)
提交回复
热议问题