プログラムで UUID (または GUID) から OID を作成する目的で、このコード ( David Clunie から) を C#に変換しようとしています。
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 に感謝します)。
ここでは、作業中の 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;
}
}