You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.7 KiB
C#

using System.Text;
namespace DS.Module.Core;
/// <summary>
/// 编码帮助类
/// </summary>
public class NumUtil
{
#region 自动生成日期编号
/// <summary>
/// 自动生成编号 201008251145409865
/// </summary>
/// <returns></returns>
public static string CreateNo()
{
Random random = new Random();
string strRandom = random.Next(1000, 10000).ToString(); //生成编号
string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom; //形如
return code;
}
#endregion
#region 生成随机推荐码
/// <summary>
/// 生成随机数的种子
/// </summary>
/// <returns></returns>
private static int getNewSeed()
{
byte[] rndBytes = new byte[4];
System.Security.Cryptography.RNGCryptoServiceProvider rng =
new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(rndBytes);
return BitConverter.ToInt32(rndBytes, 0);
}
#endregion
/// <summary>
/// 生成随机数
/// </summary>
/// <param name="length">位数</param>
/// <returns></returns>
static public string GetRandomString(int len)
{
string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
string reValue = string.Empty;
Random rnd = new Random(getNewSeed());
while (reValue.Length < len)
{
string s1 = s[rnd.Next(0, s.Length)].ToString();
if (reValue.IndexOf(s1) == -1) reValue += s1;
}
return reValue;
}
public static string GetNo()
{
string No = ""; //生成编号
DateTime now = DateTime.Now;
string Year = now.Year.ToString().Substring(2, 2);
string Month = now.Month < 10 ? "0" + now.Month : now.Month.ToString();
string Day = now.Day < 10 ? "0" + now.Day : now.Day.ToString();
string Hour = now.Hour < 10 ? "0" + now.Hour : now.Hour.ToString();
string Minute = now.Minute < 10 ? "0" + now.Minute : now.Minute.ToString();
string Second = now.Second < 10 ? "0" + now.Second : now.Second.ToString();
string Millisecond = now.Millisecond < 10 ? "0" + now.Millisecond : now.Millisecond.ToString();
Random rd = new Random();
int a = rd.Next(0, 9);
int b = rd.Next(0, 9);
int c = rd.Next(0, 9);
No = Year + Month + Day + Hour + Minute + Second + Millisecond + a + b + c;
return No;
}
public static string GetShortNo()
{
string No = ""; //生成编号
DateTime now = DateTime.Now;
string Year = now.Year.ToString().Substring(2, 2);
string Month = now.Month < 10 ? "0" + now.Month : now.Month.ToString();
string Day = now.Day < 10 ? "0" + now.Day : now.Day.ToString();
string Hour = now.Hour < 10 ? "0" + now.Hour : now.Hour.ToString();
string Minute = now.Minute < 10 ? "0" + now.Minute : now.Minute.ToString();
string Second = now.Second < 10 ? "0" + now.Second : now.Second.ToString();
string Millisecond = now.Millisecond < 10 ? "0" + now.Millisecond : now.Millisecond.ToString();
Random rd = new Random();
int a = rd.Next(0, 9);
int b = rd.Next(0, 9);
int c = rd.Next(0, 9);
No = Year + Month + Day + Hour + Minute + a + b + c;
return No;
}
public static string GenerateRandomCode(int length)
{
var result = new StringBuilder();
for (var i = 0; i < length; i++)
{
var r = new Random(Guid.NewGuid().GetHashCode());
result.Append(r.Next(0, 10));
}
return result.ToString();
}
public static Guid GenerateGuid()
{
byte[] guidArray = Guid.NewGuid().ToByteArray();
var baseDate = new DateTime(1900, 1, 1);
DateTime now = DateTime.Now;
var days = new TimeSpan(now.Ticks - baseDate.Ticks);
TimeSpan msecs = now.TimeOfDay;
byte[] daysArray = BitConverter.GetBytes(days.Days);
byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
Array.Reverse(daysArray);
Array.Reverse(msecsArray);
Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
return new Guid(guidArray);
}
/// <summary>
/// 生成50位有序Guid
/// </summary>
/// <returns></returns>
public static string GetSequentialGuid()
{
string guid = Guid.NewGuid().ToString();
var timeStr = (DateTime.Now.Ticks / 10000).ToString("x8");
var newGuid = $"{timeStr.PadLeft(13, '0')}-{guid}";
return newGuid;
}
}