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

3

5

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.174506987738820548334170905323706671133? I would prefer the solution in JavaScript. See wikipedia).

The example I converted with this online generator.

Denoting answered 19/9, 2019 at 10:37 Comment(0)
D
4

Based on other answer from @AmitJoshi; I can now answer my question:

Here is the JavaScript function:

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
}

Following are the references:

jsfiddle

Denoting answered 19/9, 2019 at 15:22 Comment(0)
S
5

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.

Shapeless answered 19/9, 2019 at 13:5 Comment(0)
D
4

Based on other answer from @AmitJoshi; I can now answer my question:

Here is the JavaScript function:

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
}

Following are the references:

jsfiddle

Denoting answered 19/9, 2019 at 15:22 Comment(0)
S
0

Just in case you want library that serves the same purpose, you may use dicomuid.js written by samucs (StackOverflow profile).

This does not require organization root prefix; this uses "2.25." as prefix. This uses Universally Unique Identifier (UUID). It converts the UUID to a single large decimal number.

Following is the code copied from 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");
Shapeless answered 29/10, 2020 at 12:9 Comment(2)
Well, dicomuid.js is using internally (hardcoded) the same libraries (uuid.js and biginteger.js). I think using the needed libraries separately and writing a small function (like in my post) is a better solution.Denoting
@Sheki: No worries. I decided to add an answer just because I came across the library and this question was relevant. I am aware that you already found a solution. Just in case someone else prefer to use library instead of code, this may help.Shapeless

© 2022 - 2024 — McMap. All rights reserved.