Base64 vs Hex: Size, Readability, When to Use
Base64 vs hex compared: size overhead (33% vs 100%), readability, a worked UTF-8 example, and a simple rule for which encoding to use.
Base64 vs Hex: Which Encoding Should You Use?
Use hex when a person will read, compare, or type the value; use Base64 when the bytes just need to travel as text in the smallest space. Both are encodings, not encryption, and both turn bytes into printable characters. They differ in size and readability. Hex spends two characters per byte (100% overhead). Base64 spends four characters per three bytes, about 33% overhead. Hex is what you see in hashes, color codes, and memory dumps because each byte stays visible as a pair of digits. Base64 is what you see in data URIs, JSON API fields, and email attachments because it is more compact. In RFC 4648 terms, hex is Base16 and Base64 uses a 64-character alphabet: the same idea with a bigger alphabet.
The Size Difference, With Real Numbers
Hex doubles the data; Base64 grows it by one third, rounded up to a multiple of four characters. The formula for Base64 is 4 x ceil(n / 3) characters for n bytes, and for hex it is 2n. For 1,000 zero bytes that is 1,336 Base64 characters against 2,000 hex characters. The rounding matters for tiny inputs: the single byte "h" is 68 in hex (2 characters) but aA== in Base64 (4 characters, two of them padding), so hex is shorter for one byte and ties at two. The string "hello" is 68656c6c6f in hex (10 characters) and aGVsbG8= in Base64 (8 characters). From three bytes up, Base64 is never longer than hex, and the saving approaches one third of the hex length as data grows.
Measured Overhead on Random Data, Wrapped Output and Gzip
We checked the formulas by encoding random bytes at several sizes in Node. At 1,000 bytes Base64 gave 1,336 characters (33.6% overhead) and hex gave 2,000 (100%). At 10 KB the figures were 13,656 against 20,480, and at 1 MiB they were 1,398,104 against 2,097,152, so Base64 settles at 33.3% and hex stays at exactly 100%. Line wrapping changes the picture: Base64 wrapped at 76 characters with CRLF line breaks, the email convention, grew to 1,434,896 characters for 1 MiB, about 36.8% overhead. Gzip narrows the gap but does not close it. Random 1 MiB gzipped to 1,053,046 bytes as Base64 and 1,136,454 as hex, against 1,048,919 for the raw bytes, so Base64 costs about 0.4% extra after compression and hex about 8.4%. Compression helps more on repetitive data, where hex still came out larger.
Why Hex Is Easier for Humans to Check
Hex is easier to inspect because one byte is always exactly two characters on a fixed boundary, while Base64 characters carry six bits each and straddle byte boundaries. In hex you can read a byte straight off the string: in 68656c6c6f, the pair 65 is the letter e. In Base64 no character lines up with a byte, so you cannot spot the third byte of aGVsbG8= by eye. That is why debuggers, hash listings, and packet dumps use hex. Hex is also case-insensitive (ff and FF are the same byte), while Base64 is case-sensitive, so lowercasing a Base64 string silently changes the data. If you mainly want to see what bytes are inside a value, convert it to hex; if you want to ship them, use Base64.
The Same Text in Both Encodings
For non-ASCII text, both encodings first take the UTF-8 bytes, so the output depends on how many bytes each character needs. The string "héllo €" is 10 bytes in UTF-8, because é takes two and the euro sign takes three. In hex that is 68c3a96c6c6f20e282ac (20 characters); in Base64 it is aMOpbGxvIOKCrA== (16 characters). The 33% saving holds, with two padding characters because 10 is not a multiple of three. PixelTools' Base64 tool encodes through UTF-8 on purpose: the browser's plain btoa() only accepts characters up to U+00FF and throws on an emoji or the euro sign, so a tool built directly on it would fail on exactly this kind of input.
How to Convert Base64 to Hex and Back
To convert Base64 to hex, decode the Base64 into bytes, then write each byte as two hexadecimal digits. Going the other way, group the hex into byte pairs and re-encode those bytes as Base64. There is no shortcut by character, because one Base64 character holds six bits and one hex digit holds four, so the groups only line up every three bytes (four Base64 characters, six hex digits). Worked example: the Base64 string SGkh decodes to the three bytes 48 69 21, which is 486921 in hex and the text "Hi!". A longer one: aGVsbG8= decodes to 68656c6c6f, the text "hello". Because this works on bytes, a value that decodes to binary data converts to hex just as cleanly as text does. To skip the manual step, paste the Base64 into the Base64 decoder and the result into the hex converter.
What the Decoder Accepts That Strict Parsers Reject
A strict Base64 decoder wants a standard alphabet, no whitespace, and correct padding, but real Base64 rarely arrives that clean, so PixelTools' decoder cleans it up first. It removes a leading data:...;base64, prefix, strips all whitespace (Base64 is commonly line-wrapped at 64 or 76 characters), maps the URL-safe characters - and _ to + and /, and restores missing = padding. After cleanup, characters outside the alphabet get an "isn't valid Base64" error, and a length one more than a multiple of four gets a "looks truncated" error. Validity is checked first so garbage is not blamed on padding. Valid Base64 can also decode to non-text: a PNG, a PDF or an encrypted blob is not UTF-8. The decoder uses strict UTF-8 mode and reports that the value may be binary data, rather than showing replacement characters that look like corruption. The same applies to hex: a hex dump of a JPEG read as text is noise because the bytes were never characters.
A Quick Rule for Choosing
Choose by who or what consumes the value: humans pick hex, transports pick Base64. Use hex for checksums and hashes people compare, color values, byte-level debugging, and anything typed by hand. Use Base64 for embedding images in data URIs, binary fields in JSON, email attachments, and certificates in PEM files, where size and line-friendliness matter. Use URL-safe Base64 when the value sits in a URL or filename, because standard Base64 contains + and / which have special meaning there. Neither protects anything: both decode instantly with no key, so never treat either as a way to hide a secret. If you need confidentiality, encrypt first, then encode the ciphertext.
Frequently asked questions
Try it on PixelTools — free, no upload needed