Created
August 17, 2021 11:16
-
-
Save PhiSYS/5359a85c56758cf1f9b910c3d385004d to your computer and use it in GitHub Desktop.
Convert Base64 encoded 16 byte binary UUID to Hex and hyphenate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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