Skip to content
gohacks.dev
Databases

UUID to BINARY(16) Converter

Convert a UUID to the hex of a BINARY(16) column and back, with MySQL's UUID_TO_BIN swap flag, SQL literals and base64.

Free · No sign-up · Your inputs stay in your browser

Input

Convert a UUID to BINARY(16) and back

Output

0190a5b2-7c3e-7d1a-9f00-2c4b6e8a1d3f

UUID

0190a5b2-7c3e-7d1a-9f00-2c4b6e8a1d3f

Hex (BINARY(16))

0190a5b27c3e7d1a9f002c4b6e8a1d3f

SQL literal

X'0190a5b27c3e7d1a9f002c4b6e8a1d3f'

WHERE id = UUID_TO_BIN('0190a5b2-7c3e-7d1a-9f00-2c4b6e8a1d3f') in MySQL 8; unhex(replace(uuid, '-', '')) elsewhere.

Base64

AZClsnw+fRqfACxLboodPw==

Storing a UUID as BINARY(16) takes 16 bytes instead of 36 for CHAR(36), which keeps InnoDB's primary key and every secondary index (each carries the primary key) smaller. The swap flag only helps version 1 UUIDs, whose time fields are stored low-first; UUIDv7 already sorts by time and should be stored unswapped.

Runs in this browser. Nothing you paste is sent to a server.

Why store UUIDs as binary

CHAR(36) takes 36 bytes plus overhead; BINARY(16) takes 16. In InnoDB every secondary index stores the primary key, so a smaller key shrinks every index on the table and keeps more of them in memory. MySQL 8 provides UUID_TO_BIN() and BIN_TO_UUID() for the conversion.

The swap flag is for version 1 only

Version 1 UUIDs store their timestamp low bits first, so consecutive ids scatter across the index; the swap flag moves the high bits to the front. UUIDv7, which is time-ordered already, and random UUIDv4 must be stored unswapped.

Share

Frequently asked questions

How do I query a BINARY(16) UUID column by hand?
WHERE id = UUID_TO_BIN('0190a5b2-7c3e-7d1a-9f00-2c4b6e8a1d3f') in MySQL 8, or WHERE id = X'0190a5b27c3e7d1a9f002c4b6e8a1d3f' in any MySQL version.
Should I use UUIDs as primary keys at all?
They suit ids generated outside the database or shared between systems. Prefer time-ordered UUIDv7 over random v4, which scatters inserts across the index and makes it fragment.
Is what I paste sent anywhere?
No. The tool runs entirely in your browser with JavaScript. Nothing you type or paste is sent to our server or stored.

Keep planning

Similar tools