Skip to content

Instantly share code, notes, and snippets.

@goeblr
Forked from jaime-olivares/DicomUID.cs
Created June 26, 2018 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goeblr/816e9cef0c1ca7f71238a131a03d9bba to your computer and use it in GitHub Desktop.
Save goeblr/816e9cef0c1ca7f71238a131a03d9bba to your computer and use it in GitHub Desktop.
Dicom UUID-derived UID generator
// A port from javascript: https://hcintegrations.ca/2014/05/14/quick-and-dirty-javascript-dicomweb-uid-generator/
public static string NewUID_dirty()
{
var r = new System.Random();
var uid = System.Text.RegularExpressions.Regex.Replace("2.25.xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx", "x", m => (r.Next()%16).ToString() );
return System.Text.RegularExpressions.Regex.Replace(uid, "y", m => (r.Next()&3|8).ToString());
}
// Trying to strictly follow the Dicom spec: ftp://medical.nema.org/medical/dicom/2013/output/html/part05.html#sect_B.2
// More details at: ISO/IEC 9834-8 / ITU-T X.667, Section 6.3
public static string NewUID()
{
var guid = new System.Guid().ToByteArray();
var bigint = new System.Numerics.BigInteger(guid);
return "2.25." + bigint.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment