Convert hexadecimal to Base32

How to convert hex to Base32 correctly: decode the hex to bytes, regroup into 5-bit values, choose the right alphabet and padding, and handle sensitive values locally.

The short answer

To convert hexadecimal to Base32, first decode the hex string into the raw bytes it represents, then encode those bytes with a Base32 alphabet. For example, the hex string 48656C6C6F is the bytes for the word Hello, and its standard Base32 form is JBSWY3DP. Don't treat the hex digits themselves as text to encode. That gives a different and usually wrong result.

This is a data encoding, not a visual text style. Unicode character substitution, CSS styling, bidirectional display and reversed or mirrored text change how characters look or which order they appear in. Hex and Base32 are two ways of writing the same underlying bytes using plain ASCII characters. Decoding the Base32 output gives back exactly the bytes the hex described.

How the conversion works

  • 1. Clean the input. Remove spaces, line breaks, colons and any 0x prefix, so only the characters 0-9 and A-F (or a-f) remain.
  • 2. Check the length. Each byte is two hex digits, so a valid byte string has an even number of digits. An odd length means a digit is missing or the value was meant as a number, not a byte sequence.
  • 3. Decode the hex into bytes.
  • 4. Take the bytes in groups of five (40 bits) and split each group into eight 5-bit values.
  • 5. Map each 5-bit value (0 to 31) to a character in the chosen Base32 alphabet.
  • 6. If the last group has fewer than five bytes, pad the output with = so its length is a multiple of eight characters, unless the destination expects unpadded output.

Every 10 hex digits (5 bytes) become 8 Base32 characters, so the Base32 form is shorter than the hex form of the same data.

Worked examples

Hex input | Bytes as text | Standard Base32 (RFC 4648)
48656C6C6F | Hello | JBSWY3DP
666F6F | foo | MZXW6===
66 | f | MY======
01 | (byte 0x01) | AE======

The last row shows a common pitfall. If you read hex 01 as the number one and convert that number to base 32, you get a single character. Encoding it as one byte gives AE======. Most tools and protocols that ask for Base32, such as authenticator secrets and RFC 4648 encoders, expect the byte-oriented result. Use number-style conversion only when the destination explicitly asks for it. It also drops leading zero bytes, which changes the data.

Converting locally

Python's standard library handles both steps:

import base64

hex_input = "48656C6C6F"
print(base64.b32encode(bytes.fromhex(hex_input)).decode("ascii"))
# JBSWY3DP

On a Linux system with xxd and GNU coreutils installed, a shell pipeline does the same thing. The -w 0 option turns off the default line wrapping of base32 output:

printf '%s' 48656C6C6F | xxd -r -p | base32 -w 0
# JBSWY3DP

Choose the right Base32 variant

Several Base32 alphabets exist, and they aren't interchangeable. Output made with one variant won't decode correctly with another, so match whatever the receiving system documents.

Variant | Alphabet | Notes
Standard (RFC 4648) | A-Z, then 2-7 | The usual default; padded with =
Extended hex (RFC 4648 base32hex) | 0-9, then A-V | Preserves sort order of the encoded bytes
Crockford Base32 | 0-9 and letters excluding I, L, O, U | Designed to reduce misreading when typed by people
z-base-32 | Lowercase, reordered alphabet | Designed for human use; not compatible with the standard alphabet

Limits that affect readability and reuse

Copy and paste

Base32 output uses only ASCII letters, digits and possibly =, so it usually survives copying between applications better than styled Unicode text. A few details still cause failures:

  • Padding: some destinations require the trailing = characters and others reject them. Some authenticator setup screens and URL-based formats commonly omit padding. Check what the target expects.
  • URLs and query strings: = has a special meaning there, so padded output may need to be percent-encoded or unpadded.
  • Case: the standard alphabet is uppercase. Many decoders also accept lowercase, but not all do, so keep uppercase unless the destination specifies otherwise.
  • Line wrapping and spaces: command-line tools may insert line breaks, and some apps display secrets in spaced groups. Strip these before decoding unless the decoder is documented to ignore them.
  • Look-alike characters: when a person retypes the value, the standard alphabet avoids 0, 1 and 8 but still contains letters that can be misread. Crockford Base32 exists to reduce this, but only use it if the destination supports it.

Accessibility

A Base32 string is ordinary text, so it doesn't have the problem that styled Unicode letters can have, where a screen reader may announce symbol names instead of letters. Long random-looking strings are still hard to read aloud, check or transcribe. Exactly how a given screen reader announces mixed letters and digits depends on the software and its settings, and no test results are given here to describe that behaviour. If people will read or type the value, displaying it in short groups can help. The application must then remove those separators before decoding. Wherever possible, offer a copy action rather than requiring manual entry.

Sensitive values

Hex and Base32 are often used for keys, tokens and one-time-password secrets. Base32 is not encryption. Anyone who sees the output can decode it back to the original bytes. If you paste a secret into a web-based converter, you send it to whoever runs that service. Unless the service documents that it converts entirely in your browser and keeps nothing, assume it may see and store the value. For anything sensitive, use a local method such as the Python or shell examples above. The same caution applies to logs, chat messages and screenshots that show the output.

When to keep the hex

Convert only when the destination actually asks for Base32. If a system accepts hex, keep it as hex: it is easier to check byte by byte, and you avoid any confusion over alphabets and padding. If the original data is readable text that only happened to be stored as hex, decoding it straight to that text may serve readers better than either encoding.