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

前端 未结 3 784
梦毁少年i
梦毁少年i 2021-01-21 09:33

How can I convert an UUID/GUID value like 8348d2c5-0a65-4560-bb24-f4f6bcba601d (that I genreated with uuid v4) in to OID/DICOM UID like 2.25.17450698773882054

3条回答
  •  攒了一身酷
    2021-01-21 10:13

    I am aware you are looking for JavaScript sample; but following is a c# code. See if you can translate it to JavaScript. The variable names and data types are self explainer which may help you while translation.

    The code below is based on this answer from @VictorDerks. There is even a faster method explained in that answer; have a look.

    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();
    }
    

    The Guid guid looks like f254934a-1cf5-47e7-913b-84431ba05b86.

    The string.Format("0{0:N}", guid) returns 0f254934a1cf547e7913b84431ba05b86. Formatting is removed and prefixed with zero.

    The BigInteger.Parse(guidBytes.... returns 322112315302124436275117686874389371782. The BigInteger.Parse will convert/parse the string to big-integer data type. The NumberStyles determine how to format.

    Looking at the question, I think you are already aware about details explained here and here.

提交回复
热议问题