我正在尝试将此代码(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的各个部分制作longs(leastSignificantBits,mostSignificantBits),然后从它们制作bigint - 为什么不直接制作BigInt呢? (因为无论如何他都会移动最重要的数字)。
任何人都可以告诉我为什么这样写的方式是什么? (免责声明:我没有尝试运行java代码,我只是想在C#中实现它)
[编辑]
原来有几个问题 - 正如Kevin Coulombe指出的那样,一个很大的问题是微软存储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;
}
}
答案 0 :(得分:1)
我认为他们这样做是因为没有简单的方法将UUID转换为字节数组以传递给Java中的BigInteger。
在C#中,这应该是你要找的东西:
String oid = "prefix" + "." + new System.Numerics.BigInteger(
System.Guid.NewGuid().ToByteArray()).ToString();
答案 1 :(得分: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