How to convert UUID
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
"Convert UUID" can mean several different things: parsing a string into a UUID object, turning a UUID back into text, or converting between UUID and a 16-byte binary form for storage or transport. The correct approach depends on which representation you are starting with and which one you need next.
Common UUID Representations
The same UUID can appear as:
- a canonical string such as
123e4567-e89b-12d3-a456-426655440000 - an in-memory UUID object
- a 16-byte binary value
- a URN such as
urn:uuid:123e4567-e89b-12d3-a456-426655440000
Most application code moves between the first three.
String to UUID and Back
In Java, parsing from a string is built into the standard library:
The string must be in the canonical hexadecimal format with hyphens. If it is malformed, UUID.fromString throws IllegalArgumentException.
In Python, the standard uuid module provides the same flow:
For application code, this is usually all you need.
UUID to Bytes and Back
Some databases and protocols store UUIDs as 16 raw bytes instead of 36-character text. In Python, that conversion is easy:
In Java, you can split the UUID into two long values and pack them into a byte array:
This is a common pattern when storing UUIDs in BINARY(16) or transmitting them in compact binary protocols.
UUIDs in Databases
The storage choice affects both readability and efficiency.
Text storage is easy to inspect in logs and SQL consoles. Binary storage is smaller and often indexes better. Some databases, such as PostgreSQL, provide a native uuid type and remove most of the tradeoff for application code.
If your database already has a native UUID type, prefer it. If it does not, store either canonical text or a 16-byte value consistently and document the byte order your system expects. That documentation matters during migrations and cross-language integrations.
URN Conversion
A UUID URN is just the canonical text with a prefix:
This is mostly relevant for interoperating with APIs or standards that expect URN format rather than plain UUID text.
Common Pitfalls
The most common mistake is treating UUID conversion as a formatting problem when it is actually a representation problem. Converting string to object is not the same thing as converting object to bytes.
Another issue is inconsistent byte ordering. If one system writes 16 raw bytes in a different layout from another system, the same UUID can appear scrambled after round-trip conversion.
Developers also sometimes strip hyphens or change letter case and assume any format is accepted everywhere. Many APIs expect the canonical form specifically.
Finally, do not store UUIDs as text just because it is easy if your database or protocol already supports a better native representation.
Summary
- UUID conversion usually means string, object, bytes, or URN conversion.
- Use standard library parsing such as
UUID.fromStringin Java oruuid.UUID(...)in Python. - Use 16-byte binary form when compact storage or transport matters.
- Prefer native database UUID types when they exist.
- Be consistent about format and byte order across systems.

