Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Base64 encoded 16 byte binary UUID to Hex and hyphenate
function UuidFromBinary(binary) {
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
let result = '';
for (let i = 0; i < bytes.length; i++) {
result += (bytes[i] & 0xFF).toString(16).padStart(2, '0');
}
return result.slice(0, 8) + '-' + result.slice(8, 12) + '-' + result.slice(12, 16) + '-' + result.slice(16, 20) + '-' + result.slice(20,32);
}
binaryUUID = "\xDE\xAD\xBE\xEF\xCA\xFE\xF0\x0D\xBA\xBE\xC0\x01\xE5\x7B\x00\xB5"; // 0xDEADBEEFCAFEF00DBABEC001E57B00B5
b64 = btoa(binaryUUID); // "3q2+78r+8A26vsAB5XsAtQ=="
console.log(b64);
binary = atob(b64);
uuid = UuidFromBinary(binary);
console.log(uuid);
// Should return "deadbeef-cafe-f00d-babe-c001e57b00b5"
// Check on https://jsfiddle.net/5roncLks/3/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment