UUID String to 16-byte blob


#1

How does one convert a UUID string like “f5a863a7-97a4-466b-bbd8-bdeeeb7923fa” into a 16 byte blob?

Thanks


#2

Off the top of my head, something like this?

function uuidConvert(uuid) {
    // Remove the UUID dashes, if any
    local parts = split(uuid, "-");
    if (parts.len() > 0) {
        uuid = "";
        foreach (part in parts) {
            uuid = uuid + part;
        }
    }

    // Pad tail end with zeros
    if (uuid.len() < 32) {
        for (local i = uuid.len() ; i < 32 ; i++) {
            uuid = uuid + "0";
        }
    }

    local b = blob(16);

    for (local i = 0 ; i < uuid.len() ; i += 2) {
        local s = uuid.slice(i, i + 2);
        local v = 0;
        foreach (c in s) {
            local n = c - '0';
            if (n > 9) n = ((n & 0x1F) - 7);
            v = (v << 4) + n;
        }

        b[i / 2] = v;
    }

    // Return the 16-byte blob
    return b;
}

You can probably refactor it down to something shorter/quicker


#3

Great thanks very much :slight_smile: