C# GUID转换成16位字符串或19位数字并确保唯一

时间:2022-12-08 16:08:19
/// <summary>
/// 根据GUID获取16位的唯一字符串
/// </summary>
/// <param name=\"guid\"></param>
/// <returns></returns>
public static string GuidTo16String()
{
long i = ;
foreach (byte b in Guid.NewGuid().ToByteArray())
i *= ((int)b + );
return string.Format(\"{0:x}\", i - DateTime.Now.Ticks);
}
/// <summary>
/// 根据GUID获取19位的唯一数字序列
/// </summary>
/// <returns></returns>
public static long GuidToLongID()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, );
}