尝试在 C# 中复制一些 PHP 代码。 PHP 代码使用 uniqid 函数,并将 more entropy 参数设置为 true。知道如何在 C# 中最好地复制它吗?
最佳答案
经过一番谷歌搜索后,我发现了 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/