c# - 将 UUID 转换为 OID,将 Java 代码转换为 C# - 这是如何工作的?

标签 c# java

我正在尝试将此代码 ( from David Clunie ) 转换为 C#,以便在我的程序中从 UUID(或 GUID)创建 OID

public static String createOIDFromUUIDCanonicalHexString(String hexString) throws IllegalArgumentException {
        UUID uuid = UUID.fromString(hexString);
        long leastSignificantBits = uuid.getLeastSignificantBits();
        long mostSignificantBits  = uuid.getMostSignificantBits();
        BigInteger decimalValue = makeBigIntegerFromUnsignedLong(mostSignificantBits);
        decimalValue = decimalValue.shiftLeft(64);
        BigInteger bigValueOfLeastSignificantBits = makeBigIntegerFromUnsignedLong(leastSignificantBits);
        decimalValue = decimalValue.or(bigValueOfLeastSignificantBits);   // not add() ... do not want to introduce question of signedness of long
        return OID_PREFIX+"."+decimalValue.toString();

我不明白为什么要从 UUID 的部分中创建 long(leastSignificantBits、mostSignificantBits),然后从它们中创建 bigint - 为什么不直接创建 BigInt? (因为无论如何他都会移动剩下的最高有效数字)。

谁能告诉我为什么这样写? (免责声明:我没有尝试运行 java 代码,我只是尝试在 C# 中实现它)

[编辑]

事实证明存在几个问题 - 正如 Kevin Coulombe 指出的那样,其中一个大问题是 Microsoft 存储 GUID 的字节顺序。java 代码似乎以明显的(从左到右)顺序获取字节,但是以两个顺序显然(也感谢 Kevin),因为在 Java 中没有简单的方法来获取整个字节数组。

这里是工作的 C# 代码,向前和(部分)向后:

    class Program
{
    const string OidPrefix = "2.25.";

    static void Main(string[] args)
    {
        Guid guid = new Guid("000000FF-0000-0000-0000-000000000000");
        //Guid guid = new Guid("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
        Console.WriteLine("Original guid: " + guid.ToString());
        byte[] guidBytes = StringToByteArray(guid.ToString().Replace("-", ""));
        BigInteger decimalValue = new BigInteger(guidBytes);
        Console.WriteLine("The OID is " + OidPrefix + decimalValue.ToString());
        string hexGuid = decimalValue.ToHexString().PadLeft(32, '0');//padded for later use
        Console.WriteLine("The hex value of the big int is " + hexGuid);
        Guid testGuid = new Guid(hexGuid);
        Console.WriteLine("This guid should match the orginal one: " + testGuid);
        Console.ReadKey();
    }

    public static byte[] StringToByteArray(String hex)
    {
        int NumberChars = hex.Length;
        byte[] bytes = new byte[NumberChars / 2];
        for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
        return bytes;
    }
}

最佳答案

我认为他们这样做是因为没有简单的方法将 UUID 转换为字节数组以传递给 Java 中的 BigInteger。

看这个:GUID to ByteArray

在 C# 中,这应该是您正在寻找的:

String oid = "prefix" + "." + new System.Numerics.BigInteger(
        System.Guid.NewGuid().ToByteArray()).ToString();

关于c# - 将 UUID 转换为 OID,将 Java 代码转换为 C# - 这是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17870071/

相关文章:

java - 如何从另一个 fragment 调用方法

c# - NPOI Excel 数字格式未显示在 asp.net 的 Excel 工作表中

c# - 在 Quartz.net 中,如何在使用单个 IJob 时防止作业同时运行?

c# - RSA加解密结果有3个问号

c# - 动态编译以提高性能

java - 这个 while 循环有什么问题吗?

java - 如何找到两个任意多边形之间的重叠区域

java - 从具有千位分隔符的字符串中解析数字

C# - 有限列表还是有限列表?

java - 用随机数填充矩阵,垂直或水平不重复