Question: Delphi Code for converting a UUID to an OID

Mar 4, 2013

This is sort of turning into a pretty common question - I had no idea that so many vendors still use delphi: if you are using delphi, how do you convert a GUID to it’s OID representation? Well, here’s the code: The algorithm is simple in concept

  • Remove the “-“ seperators from the GUID
  • Treat the resulting string as a hexadecimal number
  • Convert the number to a decimal number
  • Prepend the OID 2.25 to the number

Reference: see http://www.itu.int/ITU-T/studygroups/com17/oid/X.667-E.pdf

Although the method is simple, the implementation can be a challenge because of the size of the numbers involved. This is a pascal implementation. The implementation depends on bignum, from http://webtweakers.com/swag/DELPHI/0464.PAS.html

 



Function GUIDAsOIDRight(Const aGUID : TGUID) : String;
var
  sGuid, s : String;
  r1, r2, r3, r4 : int64;
  c : integer;
  b1, b2, b3, b4, bs : TBigNum;
Begin
  sGuid := GUIDToString(aGuid);
  s := copy(sGuid, 30, 8);
  Val('$'+s, r1, c);
  s := copy(sGuid, 21, 4)+copy(sGuid, 26, 4);
  Val('$'+s, r2, c);
  s := copy(sGuid, 11, 4)+copy(sGuid, 16, 4);
  Val('$'+s, r3, c);
  s := copy(sGuid, 2, 8);
  Val('$'+s, r4, c);

  b1 := TBigNum.Create;
  b2 := TBigNum.Create;
  b3 := TBigNum.Create;
  b4 := TBigNum.Create;
  bs := TBigNum.Create;
  Try
    b1.AsString := IntToStr(r1);
    b2.AsString := IntToStr(r2);
    b3.AsString := IntToStr(r3);
    b4.AsString := IntToStr(r4);
    bs.AsString := '4294967296';
    b2.Multiply(bs);
    bs.AsString := '18446744073709551616';
    b3.Multiply(bs);
    bs.AsString := '79228162514264337593543950336';
    b4.Multiply(bs);
    b1.Add(b2);
    b1.Add(b3);
    b1.Add(b4);
    result := '2.25.'+b1.AsString;
  Finally
    b1.Free;
    b2.Free;
    b3.Free;
    b4.Free;
    bs.Free;
  End;
end;