c# - 将 Guid 字符串转换为 BigInteger,反之亦然

标签 c# guid biginteger

我可以使用下面的方法将 Guid String 转换为 BigInteger。如何将 BigInteger 转换回 Guid 字符串。

using System;
using System.Numerics;

class Class1
{       
    public static BigInteger GuidStringToBigInt(string guidString)
    {
        Guid g = new Guid(guidString);
        BigInteger bigInt = new BigInteger(g.ToByteArray());
        return bigInt;
    }

    static void Main(string[] args)
    {
        string guid1 = "{90f0fb85-0f80-4466-9b8c-2025949e2079}";

        Console.WriteLine(guid1);
        Console.WriteLine(GuidStringToBigInt(guid1));
        Console.ReadKey();
    }
}

最佳答案

请检查:

public static Guid ToGuid(BigInteger value)
{
     byte[] bytes = new byte[16];
     value.ToByteArray().CopyTo(bytes, 0);
     return new Guid(bytes);
}

编辑:Working Fiddle

关于c# - 将 Guid 字符串转换为 BigInteger,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54353695/

相关文章:

c# - 在没有头文件的情况下声明 COM 接口(interface)指针

guid - 如何获取 Cloud Foundry 应用程序实例的 GUID?

algorithm - 是否有标准算法将 guid 编码为 base 107 或更大?

java - Java 中 C 的这些等效数据类型是什么?

c++ - 大整数类的位操作?

c# - 您可以强制 Parallel.Invoke 使用多线程吗?

c# - 在 .NET Framework 4.6.2 中,FormattedText() 已过时,我该如何修复它

python - 在 App Engine 上生成 GUID 的好方法?

java - 我需要优化 BigInteger 的 toString() 方法

c# - 在构造函数中实例化对象