41.21.2 UUID Conversion

To use a UUID, typically it is converted to a string, and sometimes to a binary representation. This is done using uuid-to-string function.

Function: uuid-to-string uuid

Return the standard string value, with hex values and dashes, of Lisp object uuid.

(uuid-to-string (uuid-v4))     ; "230156d4-488a-45d1-93c9-507ec8be37ca"

Binary can also be generated, which generates a unibyte string, using uuid-to-bytes, and a numeric representation of a UUID can be generated with uuid-to-number.

Function: uuid-to-bytes uuid

Return a unibyte string representing the binary value of uuid.

Function: uuid-to-number uuid

Return a numeric representation of the value of uuid.

String UUIDs can also be converted to a UUID Lisp object, with uuid-from-string, and bytes can be converted with uuid-from-bytes. These work with all UUIDs, not just the ones this module can generate.

Function: uuid-from-string uuid-str

Return a UUID Lisp object that represents uuid-str.

Function: uuid-from-bytes uuid-bytes

Return a UUID Lisp object that represents uuid-bytes, which is expected to be a unibyte string.

Valid UUIDs have types, which can be used with cl-typep, cl-check-type and cl-typecase. The type is specified with the type specifier (uuid-v <version>). version can be any version of UUID, but we make sure the UUID variant is correctly 2; any other value is not a valid version.

(when (cl-typep my-uuid '(uuid-v 4))
    (process-v4-uuid my-uuid))

(defun process-v4-uuid (uuid)
    ;; Error if not the correct type
    (cl-check-type uuid (uuid-v 4))
    ;; Proceed to process the UUIDv4.
    (more-processing uuid))