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

Mer*_*rmi 2 c# guid biginteger

我可以使用下面的方法将 Guid 字符串转换为 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Der*_*ğlu 5

请检查这个:

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

编辑:工作小提琴

  • 这不正确。从一个到另一个尝试 64bf6328-de36-48a1-af69-378183c37a00 抛出“GUID 的字节数组必须正好是 16 个字节长”这整件事都闻起来了。 (3认同)