DICOMでUIDを作成する方法は2つあります。1つは登録済みのUIDルートに基づいており、もう1つはUUIDに基づいています。後者の方法は、2012年にCP-1156でDICOM標準に追加されました。スタディUID、シリーズUID、SOPインスタンスUIDなどのUIDは、UUIDをDICOMUIDに変換することで作成できます。
ほとんどのプログラミング言語には、UUIDを作成するためのサポートが組み込まれています。以下のサンプルコードコードは、GUID値に基づいてC#で有効なDICOMUIDを作成します。
public static string GuidToUidStringUsingStringAndParse(Guid value)
{
var guidBytes = string.Format("0{0:N}", value);
var bigInteger = BigInteger.Parse(guidBytes, NumberStyles.HexNumber);
return string.Format(CultureInfo.InvariantCulture, "2.25.{0}", bigInteger);
}
次の方法も同じですが、約5倍高速です。
public static string ConvertGuidToUuidInteger(ref Guid value)
{
// ISO/IEC 9834-8, paragraph 6.3 (referenced by DICOM PS 3.5, B.2) defines how
// to convert a UUID to a single integer value that can be converted back into a UUID.
// The Guid.ToByteArray Method returns the array in a strange order (see .NET docs),
// BigInteger expects the input array in little endian order.
// The last byte controls the sign, add an additional zero to ensure
// the array is parsed as a positive number.
var octets = value.ToByteArray();
var littleEndianOrder = new byte[]
{ octets[15], octets[14], octets[13], octets[12], octets[11], octets[10], octets[9], octets[8],
octets[6], octets[7], octets[4], octets[5], octets[0], octets[1], octets[2], octets[3], 0 };
return "2.25." + new BigInteger(littleEndianOrder).ToString(CultureInfo.InvariantCulture);
}