DICOM 中有两种创建 UID 的方法。一种基于已注册的 UID 根,另一种基于 UUID。后一种方法于 2012 年通过 CP-1156 添加到 DICOM 标准中。可以通过将 UUID 转换为 DICOM UID 来创建学习 UID、系列 UID、SOP 实例 UID 等内容的 UID。
大多数编程语言都支持创建 UUID。下面的示例代码代码基于 GUID 值在 C# 中创建有效的 DICOM UID。
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);
}