.NET Short Unique Identifier

前端 未结 23 1110
忘掉有多难
忘掉有多难 2020-12-07 11:27

I need a unique identifier in .NET (cannot use GUID as it is too long for this case).

Do people think that the algorithm used here is a good candidate or do you have

23条回答
  •  自闭症患者
    2020-12-07 11:58

    here my solution, is not safe for concurrency, no more of 1000 GUID's per seconds and thread safe.

    public static class Extensors
    {
    
        private static object _lockGuidObject;
    
        public static string GetGuid()
        {
    
            if (_lockGuidObject == null)
                _lockGuidObject = new object();
    
    
            lock (_lockGuidObject)
            {
    
                Thread.Sleep(1);
                var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                var epochLong = Convert.ToInt64((DateTime.UtcNow - epoch).TotalMilliseconds);
    
                return epochLong.DecimalToArbitrarySystem(36);
    
            }
    
        }
    
        /// 
        /// Converts the given decimal number to the numeral system with the
        /// specified radix (in the range [2, 36]).
        /// 
        /// The number to convert.
        /// The radix of the destination numeral system (in the range [2, 36]).
        /// 
        public static string DecimalToArbitrarySystem(this long decimalNumber, int radix)
        {
            const int BitsInLong = 64;
            const string Digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    
            if (radix < 2 || radix > Digits.Length)
                throw new ArgumentException("The radix must be >= 2 and <= " + Digits.Length.ToString());
    
            if (decimalNumber == 0)
                return "0";
    
            int index = BitsInLong - 1;
            long currentNumber = Math.Abs(decimalNumber);
            char[] charArray = new char[BitsInLong];
    
            while (currentNumber != 0)
            {
                int remainder = (int)(currentNumber % radix);
                charArray[index--] = Digits[remainder];
                currentNumber = currentNumber / radix;
            }
    
            string result = new String(charArray, index + 1, BitsInLong - index - 1);
            if (decimalNumber < 0)
            {
                result = "-" + result;
            }
    
            return result;
        }
    

    code not optimized, just sample!.

提交回复
热议问题