如何在JavaScript中将UUID/GUID转换为OID/DICOM UID?

How to convert UUID/GUID to OID/DICOM UID in JavaScript?

如何转换 UUID/GUID 值,如 8348d2c5-0a65-4560-bb24-f4f6bcba601d(我用 uuid v4) in to OID/DICOM UID like 2.25.174506987738820548334170905323706671133? I would prefer the solution in JavaScript. See wikipedia 生成)。

我用this在线生成器转换的例子。

我知道您正在寻找 JavaScript 样本;但以下是 C# 代码。看看你能不能把它翻译成 JavaScript。变量名和数据类型是自我解释器,可以在翻译时帮助你。

以下代码基于@VictorDerks 的this 回答。该答案中甚至解释了一种更快的方法;看看。

public string GenerateUidFromGuid()
{
    Guid guid = Guid.NewGuid();
    string strTemp = "";
    StringBuilder uid = new StringBuilder(64, 64);
    uid.Append("2.25.");

    //This code block is important------------------------------------------------
    string guidBytes = string.Format("0{0:N}", guid);
    BigInteger bigInteger = BigInteger.Parse(guidBytes, NumberStyles.HexNumber);
    strTemp = string.Format(CultureInfo.InvariantCulture, "{0}", bigInteger);
    uid.Append(strTemp);
    //This code block is important------------------------------------------------

    return uid.ToString();
}

Guid guid 看起来像 f254934a-1cf5-47e7-913b-84431ba05b86

string.Format("0{0:N}", guid) returns 0f254934a1cf547e7913b84431ba05b86。格式被删除并以零为前缀。

BigInteger.Parse(guidBytes.... returns 322112315302124436275117686874389371782BigInteger.Parse will convert/parse the string to big-integer data type. The NumberStyles 决定如何格式化。

看问题,我想你已经知道详细解释了here and here

基于@AmitJoshi 的;我现在可以回答我的问题了:

这里是 JavaScript 函数:

function GenerateUidFromGuid(){
   var guid = uuid.v4();                         //Generate UUID using node-uuid *) package or some other similar package
   var guidBytes = `0${guid.replace(/-/g, "")}`; //add prefix 0 and remove `-`
   var bigInteger = bigInt(guidBytes,16);        //As big integer are not still in all browser supported I use BigInteger **) packaged to parse the integer with base 16 from uuid string
   return `2.25.${bigInteger.toString()}`;       //Output the previus parsed integer as string by adding `2.25.` as prefix
}

参考文献如下:

jsfiddle

以防万一您需要具有相同用途的库,您可以使用 dicomuid.js written by samucs (Whosebug profile)。

这不需要组织根前缀;这使用“2.25”。作为前缀。这使用通用唯一标识符 (UUID)。它将 UUID 转换为单个大十进制数。

以下是从github复制的代码:

// Create new DICOM UID.
// Result will be like 2.25.176371623884904210764200284661930180516
var uid1 = DICOMUID.create();

// Create DICOM UID from a RFC4122 v4 UUID.
// Result for line below is 2.25.329800735698586629295641978511506172918
var uid2 = DICOMUID.create("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");