Converting UUIDs to bytes with uuid
parse and stringify convert between the text form of a UUID and its 16 raw bytes, for binary storage or protocols.
How it works
- String to bytes.
parse(id)returns aUint8Arrayof 16 values. The example prints them as decimals and as hex pairs, which line up with the hex in the string. [1] - Bytes back to a string.
stringify(bytes)gives back the original UUID. A second argument picks the offset to start reading from. [1] - Write into a buffer. Passing a buffer and offset to
v4writes the bytes there instead of returning a string. Two UUIDs share one 32-byte array andstringify(buffer, 16)reads the second. [1]
Run and edit it
It runs in your browser, in a sandboxed frame that cannot see this site; the packages load from esm.sh only after you press Run.
import { parse, stringify, v4 } from 'uuid'
const id = '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'
// String -> 16 bytes
const bytes = parse(id)
console.log('uuid: ', id)
console.log('type: ', bytes.constructor.name, 'of length', bytes.length)
console.log('bytes: ', Array.from(bytes).join(', '))
console.log('hex pairs: ', Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join(' '))
// Bytes -> string
console.log('stringify: ', stringify(bytes))
// Write two UUIDs into one 32-byte buffer, then read each by offset
const buffer = new Uint8Array(32)
v4(undefined, buffer, 0)
v4(undefined, buffer, 16)
console.log('buffer[0..15]: ', stringify(buffer, 0))
console.log('buffer[16..31]:', stringify(buffer, 16))
// parse() throws on invalid input
try {
parse('not-a-uuid')
} catch (err) {
console.log('parse("not-a-uuid") throws:', err.name)
}
Change the id string and compare its hex pairs with the bytes line. Code written by AgentGid [1].
Change the id string and compare its hex pairs with the bytes line. Code written by AgentGid [1].Checked Oct 12, 2026: it starts without errors in Chromium with uuid 14.0.3. Needs WebGL.
More uuid examples
Random UUIDs with uuid v4 Time-based UUIDs with uuid v7 Name-based UUIDs with uuid v5 Validating UUIDs with uuid
Sources
Explanations written by AgentGid from the official docs; the numbers in brackets point here. The code marked "written by AgentGid" is ours and free to copy.