Base32Hex Encoder and Decoder

Extended hex Base32 variant used in DNSSEC NSEC3 and sortable IDs

Encode and decode using the RFC 4648 Base32hex extended-hex alphabet (0-9 then A-V). Unlike standard Base32, encoded output sorts in the same order as the raw bytes, which is why DNSSEC NSEC3 relies on it. Runs in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

How is Base32hex different from standard Base32?

Both pack 5 bits per character, but Base32hex uses the alphabet 0 to 9 followed by A to V, whereas standard Base32 uses A to Z then 2 to 7. The hex variant is chosen so that encoded strings keep the same sort order as the binary data they represent.

Base32hex is the extended-hex variant of Base32 from RFC 4648. It encodes the same 5-bits-per-character data, but with an alphabet that preserves sort order, which makes it the format behind DNSSEC NSEC3 records and similar order-sensitive keys.

Standard Base32 vs Base32hex — what actually differs

Both encodings pack exactly 5 bits into each ASCII character, producing strings that are 60% larger than their binary source but entirely safe for text contexts. The only difference is the alphabet:

PositionStandard Base32Base32hex
0–25A–Z0–9 (10 chars) then A–V (16 chars)
26–312–7
Padding==

Standard Base32 starts with letters, which means the encoded bytes 0x00 and 0xFF both encode to strings starting with A and 7 respectively — the sort order of encoded strings does not match the sort order of the raw bytes.

Base32hex starts with digits 0 through 9 and then continues with A through V, exactly mirroring how hexadecimal ordering works. This means if you sort two Base32hex strings lexicographically, the result is the same as sorting the underlying binary values numerically.

How it works

Encoding is identical to standard Base32 except for the symbol table. Bytes are read as a bit stream and chopped into 5-bit groups, each mapped through the extended-hex alphabet:

alphabet = 0123456789ABCDEFGHIJKLMNOPQRSTUV   (index 0..31)
emit one character per 5 bits, zero-fill the last group
pad with '=' to a multiple of 8 characters (optional)

Because index 0 maps to 0 and the values climb monotonically through the digits and then the letters, the lexical order of two encoded strings matches the numeric order of the bytes they encode.

Why DNSSEC NSEC3 requires sort-order preservation

DNSSEC’s NSEC3 record type proves that a domain name does not exist by pointing from one hashed name to the next hashed name in sorted order, covering all names in between. DNS software must be able to walk these hashes in order to verify the proof. If the encoding did not preserve sort order, a DNS resolver would have to decode every hash before it could determine position — defeating the purpose of the compact encoding.

Base32hex was specified in RFC 4648 §7 precisely for this use case: an encoding that is case-insensitive, URL-safe, avoids punctuation that confuses zone files, and sorts correctly. NSEC3 records in every DNSSEC-signed zone you look up use this format.

When to use Base32hex vs. alternatives

NeedBest choiceReason
Sort-order preserved IDsBase32hexOnly Base32 variant with this property
DNSSEC compatibilityBase32hexMandated by NSEC3 spec
General binary-to-textStandard Base32Wider library support
Shortest outputBase6433% overhead vs. 60%
Fully URL-safe, no paddingBase64urlCompact and URL-friendly

Decoding notes

Any character outside 0–9 and A–V is invalid in Base32hex — specifically, the letters W, X, Y, and Z are invalid here even though they are valid in standard Base32. The decoder names the offending character so you can spot mismatched-alphabet errors quickly.