如何在JavaScript中将UUID/GUID转换为OID/DICOM UID?
Convert UUID v4 to DICOM OID (2.25.x) in JavaScript
Got it, let's break down how to convert a UUID v4 to a DICOM-compliant OID using JavaScript. The key here is understanding that DICOM uses the 2.25 root OID for UUID-based identifiers (per ISO/IEC 9834-8), which maps directly to the 128-bit value of the UUID converted to a decimal integer.
Step-by-Step Explanation
- UUID Cleanup: A UUID v4 is a 128-bit number represented as 32 hex characters split by hyphens. First, strip those hyphens to get the raw hex string.
- Handle Large Numbers: Since 128-bit values are way too big for JavaScript's standard
Numbertype (which loses precision beyond 2^53), we useBigIntto safely convert the hex string to a decimal integer. - Build the Valid OID: Append the decimal integer to the
2.25root to form the final DICOM UID.
Complete JavaScript Function
function uuidToDicomOid(uuid) { // Remove hyphens and normalize to lowercase hex const cleanedUuid = uuid.replace(/-/g, '').toLowerCase(); // Basic validation for valid UUID format if (!/^[0-9a-f]{32}$/.test(cleanedUuid)) { throw new Error('Invalid UUID input. Please provide a valid UUID v4 string.'); } // Convert hex string to 128-bit BigInt const uuidBigInt = BigInt(`0x${cleanedUuid}`); // Construct and return the DICOM OID return `2.25.${uuidBigInt.toString()}`; } // Test with your example UUID const exampleUuid = '8348d2c5-0a65-4560-bb24-f4f6bcba601d'; const dicomOid = uuidToDicomOid(exampleUuid); console.log(dicomOid); // Output: 2.25.174506987738820548334170905323706671133
Key Notes
- BigInt Compatibility: This works in all modern browsers and Node.js 10+ (environments that support
BigInt). - Validation: The function includes basic checks to ensure the input is a valid UUID. You can add stricter checks (like verifying UUID v4 version bits) if needed for your use case.
- DICOM Compliance: The
2.25root is officially reserved for UUID-based UIDs, so this output will be fully valid for DICOM systems.
内容的提问来源于stack exchange,提问作者Sheki
相关产品推荐
相关产品推荐

