UUID
Conversion Methods
Programming
Data Management
Coding Tutorial

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:

java
1import java.util.UUID;
2
3UUID id = UUID.fromString("123e4567-e89b-12d3-a456-426655440000");
4String text = id.toString();
5
6System.out.println(id);
7System.out.println(text);

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:

python
1import uuid
2
3u = uuid.UUID("123e4567-e89b-12d3-a456-426655440000")
4print(str(u))

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:

python
1import uuid
2
3u = uuid.UUID("123e4567-e89b-12d3-a456-426655440000")
4raw = u.bytes
5round_trip = uuid.UUID(bytes=raw)
6
7print(len(raw))
8print(round_trip)

In Java, you can split the UUID into two long values and pack them into a byte array:

java
1import java.nio.ByteBuffer;
2import java.util.UUID;
3
4UUID id = UUID.fromString("123e4567-e89b-12d3-a456-426655440000");
5
6ByteBuffer buffer = ByteBuffer.allocate(16);
7buffer.putLong(id.getMostSignificantBits());
8buffer.putLong(id.getLeastSignificantBits());
9byte[] raw = buffer.array();
10
11ByteBuffer read = ByteBuffer.wrap(raw);
12UUID roundTrip = new UUID(read.getLong(), read.getLong());
13
14System.out.println(roundTrip);

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:

python
1import uuid
2
3u = uuid.UUID("123e4567-e89b-12d3-a456-426655440000")
4urn = u.urn
5print(urn)

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.fromString in Java or uuid.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.

Course illustration
Course illustration

All Rights Reserved.