根據時間生成10位唯一性編碼(最少9位)
阿新 • • 發佈:2019-02-13
private static char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToArray();
public static string CreatCode()
{
int Year = Convert.ToInt32(DateTime.Now.Year.ToString());//2位
int Month = Convert.ToInt32(DateTime.Now.Month.ToString());//1位
int Day = Convert.ToInt32(DateTime.Now.Day.ToString());//1位
int Hour = Convert.ToInt32(DateTime.Now.Hour.ToString());//1位
int Minute = Convert.ToInt32(DateTime.Now.Minute.ToString());//1位
int Second = Convert.ToInt32(DateTime.Now.Second.ToString());//1位
int Millisecond = Convert.ToInt32(DateTime.Now.Millisecond.ToString());//2位
List<int> IntTimes = new List<int>();
IntTimes.Add(Year);
IntTimes.Add(Month);
IntTimes.Add(Day);
IntTimes.Add(Hour);
IntTimes.Add(Minute);
IntTimes.Add(Second);
IntTimes.Add(Millisecond);
string code = TimeToString64(IntTimes) + GetUniqueKey(1);
Thread.Sleep(100);
return code;
}
/// <summary>
/// 將時間轉化為對應62位字元序列中 的字元編碼
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string TimeToString64(List<int> number)
{
int size = 10;
StringBuilder result = new StringBuilder(size);
foreach (int item in number)
{
if (item / 62 == 0)
{
result.Append(chars[item % (chars.Length - 1)]);
}
else
{
result.Append(chars[(item / 62) % (chars.Length - 1)]);
result.Append(chars[item % (chars.Length - 1)]);
}
}
return result.ToString();
}
private static string GetUniqueKey(int size)
{
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
byte[] data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
}
public static string CreatCode()
{
int Year = Convert.ToInt32(DateTime.Now.Year.ToString());//2位
int Month = Convert.ToInt32(DateTime.Now.Month.ToString());//1位
int Day = Convert.ToInt32(DateTime.Now.Day.ToString());//1位
int Hour = Convert.ToInt32(DateTime.Now.Hour.ToString());//1位
int Minute = Convert.ToInt32(DateTime.Now.Minute.ToString());//1位
int Second = Convert.ToInt32(DateTime.Now.Second.ToString());//1位
int Millisecond = Convert.ToInt32(DateTime.Now.Millisecond.ToString());//2位
List<int> IntTimes = new List<int>();
IntTimes.Add(Year);
IntTimes.Add(Month);
IntTimes.Add(Day);
IntTimes.Add(Hour);
IntTimes.Add(Minute);
IntTimes.Add(Second);
IntTimes.Add(Millisecond);
string code = TimeToString64(IntTimes) + GetUniqueKey(1);
Thread.Sleep(100);
return code;
}
/// <summary>
/// 將時間轉化為對應62位字元序列中 的字元編碼
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string TimeToString64(List<int> number)
{
int size = 10;
StringBuilder result = new StringBuilder(size);
foreach (int item in number)
{
if (item / 62 == 0)
{
result.Append(chars[item % (chars.Length - 1)]);
}
else
{
result.Append(chars[(item / 62) % (chars.Length - 1)]);
result.Append(chars[item % (chars.Length - 1)]);
}
}
return result.ToString();
}
private static string GetUniqueKey(int size)
{
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
byte[] data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{
result.Append(chars[b % (chars.Length - 1)]);
}
return result.ToString();
}