GUID獲取16位19位22位的唯一字串

BIGBOY_GU發表於2018-08-16

/// <summary>

    /// 根據GUID獲取16位的唯一字串

    /// </summary>

    /// <param name=”guid”></param>

    /// <returns></returns>

    public static string GuidTo16String()

    {

        long i = 1;

        foreach (byte b in Guid.NewGuid().ToByteArray())

            i *= ((int)b + 1);

        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, 0);

    }

/// <summary>

    /// 生成22位唯一的數字 併發可用

    /// </summary>

    /// <returns></returns>

    public static string GenerateUniqueID()

    {

        System.Threading.Thread.Sleep(1); //保證yyyyMMddHHmmssffff唯一

        Random d = new Random(BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0));

        string strUnique = DateTime.Now.ToString(“yyyyMMddHHmmssffff”) + d.Next(1000, 9999);

        return strUnique;

    }

相關文章