It is so painful for me when I found there is no code did such so simple thing on the Internet in a simple way. So I did it.
function guidToBytes(guid) {
var bytes = [];
guid.split('-').map((number, index) => {
var bytesInChar = index < 3 ? number.match(/.{1,2}/g).reverse() : number.match(/.{1,2}/g);
bytesInChar.map((byte) => { bytes.push(parseInt(byte, 16));})
});
return bytes;
}
guid id format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
If you have access to
Buffer()
this is a one liner.EDIT: So fun fact, this one liner doesn't work!
C# and others don't encode GUID/UUID bytes the way its done visually. They actually change the endianness of the first few parts. Funny enough, this was accounted for in the code above!