c# - PHP 相当于 C# 中的 uniqid()

标签 c# php

尝试在 C# 中复制一些 PHP 代码。 PHP 代码使用 uniqid 函数,并将 more entropy 参数设置为 true。知道如何在 C# 中最好地复制它吗?

http://us2.php.net/uniqid

最佳答案

经过一番谷歌搜索后,我发现了 PHP 的 uniqid 是如何工作的,并在 c# 中实现了它:

private string GetUniqID()
{
    var ts = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0));
    double t = ts.TotalMilliseconds / 1000;

    int a = (int)Math.Floor(t);
    int b = (int)((t - Math.Floor(t)) * 1000000);

    return a.ToString("x8") + b.ToString("x5");
}

这段代码提供了完全相同的结果,唯一的区别是没有实现 uniqid() 的额外参数。

关于c# - PHP 相当于 C# 中的 uniqid(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1316026/

相关文章:

c# - 在没有资源的类上实现 IDisposable 有什么好处吗?

c# - WPF 两个 Line 对象的交点坐标

c# - 如何用静态方法最小化静态类(UnitOfWork 案例)?

php - html Quickform 中的客户端确认弹出窗口

php - symfony 中没有通过 Doctrine 连接数据库

php - 秦牛:始终返回“405不允许”

c# - 为什么 CancellationTokenSource.Token.register 回调被所有请求共享?

c# - 为什么 CodeContracts 警告我 "requires unproven: index < @this.Count"即使我已经检查了计数?

javascript - angular如何在html中动态附加变量

php - 是否可以对 $_POST 变量进行分组?