1

我正在尝试将此代码(来自 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;
    }
}
4

2 回答 2

1

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

看到这个:GUID 到 ByteArray

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

String oid = "prefix" + "." + new System.Numerics.BigInteger(
        System.Guid.NewGuid().ToByteArray()).ToString();
于 2013-07-25T23:05:39.510 回答
0
String oid_prefix = "2.25"
String hexString = "f81d4fae-7dec-11d0-a765-00a0c91e6bf6"
UUID uuid = UUID.fromString(hexString);
long leastSignificantBits = uuid.getLeastSignificantBits();
long mostSignificantBits  = uuid.getMostSignificantBits();
mostSignificantBits = mostSignificantBits & Long.MAX_VALUE;
BigInteger decimalValue = BigInteger.valueOf(mostSignificantBits);
decimalValue = decimalValue.setBit(63);
decimalValue = decimalValue.shiftLeft(64);
leastSignificantBits = leastSignificantBits & Long.MAX_VALUE;
BigInteger bigValueLeastSignificantBit = BigInteger.valueOf(leastSignificantBits);
bigValueLeastSignificantBit = bigValueLeastSignificantBit.setBit(63);
decimalValue = decimalValue.or(bigValueLeastSignificantBit);
println "oid is = "+oid_prefix+"."+decimalValue
于 2015-07-31T08:52:01.480 回答