BLOG
Base64 vs Hex vs Binary: Encoding Formats Explained
You have a byte sequence — maybe a cryptographic key, a small image, or a hash digest — and you need to represent it as text. You have three common options: Base64, hexadecimal, or binary. Each produces a different-looking string from the same input, each has a different size overhead, and each fits different contexts. Here is how they compare and when each one makes sense.
TL;DR — Quick Comparison
| Feature | Base64 | Hexadecimal | Binary | Winner |
|---|---|---|---|---|
| Size overhead | ~33% | 100% | 700% | Base64 |
| Characters used | A-Z, a-z, 0-9, +, / | 0-9, a-f | 0, 1 | — |
| Human readability | Low | Medium | Very low | Hex |
| Byte-level inspection | Hard | Easy (2 chars = 1 byte) | Easy (8 chars = 1 byte) | Hex |
| URL safe | Needs variant (base64url) | Yes | Yes | Hex |
| Common in APIs/JSON | Very common | Common | Rare | Base64 |
| Debugging ease | Requires decoding | Read directly | Tedious | Hex |
What Is Base64 Encoding?
Base64 takes every 3 bytes of input and encodes them as 4 ASCII characters, using an alphabet of 64 characters (A-Z, a-z, 0-9, plus sign, forward slash, and equals sign for padding). The result is a text string that can safely travel through systems designed for text — email (MIME), JSON payloads, HTML data URIs, and XML documents. The overhead is roughly 33%: a 300-byte input becomes 400 characters of Base64.
Base64 is by far the most space-efficient text encoding for binary data. That efficiency is why it became the standard for embedding images in CSS (data:image/png;base64,...), transmitting file attachments in email, and passing binary tokens in HTTP headers.
What Is Hexadecimal Encoding?
Hexadecimal (hex) represents each byte as two characters from the set 0-9 and a-f. The byte value 255 becomes ff. The byte value 0 becomes 00. This 1-byte-to-2-characters mapping makes hex the most intuitive format for inspecting raw data byte by byte. When you see 48656c6c6f, you can mentally break it into 48 65 6c 6c 6f and recognize the ASCII codes for "Hello."
The trade-off: hex doubles the size of the data. A 32-byte SHA-256 hash becomes a 64-character hex string. That is 33% larger than the same hash in Base64 (which would be 44 characters). For hash digests and small values, the extra size is negligible. For large binary blobs, it adds up.
What Is Binary Encoding?
Binary encoding represents each byte as 8 characters of 0s and 1s. The letter "A" (byte value 65) becomes 01000001. This is the rawest possible text representation of data — you are literally seeing every bit. The overhead is enormous: each byte becomes 8 characters, so a 100-byte input becomes 800 characters of text.
Binary representation is rarely used for data transmission or storage. Its value is educational and diagnostic: understanding bitwise operations, examining network protocol headers, or debugging hardware registers. When you need to see individual flag bits, binary is the only format that shows them directly.
Side-by-Side Comparison
Size Efficiency
Take a concrete example: a 256-bit (32-byte) cryptographic key.
- Raw bytes: 32 bytes
- Base64: 44 characters (~37% overhead)
- Hex: 64 characters (100% overhead)
- Binary: 256 characters (700% overhead)
For anything that travels over a network or gets stored in a database, Base64 is the clear winner on size. Hex is acceptable for short values like hashes and UUIDs. Binary is impractical for anything beyond educational display.
Readability and Debugging
Hex wins here. Each pair of hex characters maps to exactly one byte, making it trivial to identify specific bytes in a sequence. Network tools like Wireshark display packet data in hex for exactly this reason. Base64 is opaque — you cannot glance at a Base64 string and identify individual bytes without decoding it. Binary is technically readable at the bit level, but long binary strings are exhausting to parse visually.
URL and JSON Safety
Standard Base64 uses + and /, which have special meaning in URLs and can cause issues in JSON depending on the context. Base64url (RFC 4648) replaces them with - and _ to solve this. Hex uses only alphanumeric characters and is inherently URL-safe. Binary (0s and 1s) is also URL-safe but rarely used in URLs.
Ecosystem Usage
Base64 dominates in API payloads, email encoding (MIME), JWTs, data URIs, and certificate encoding (PEM). Hex dominates in hash digests (SHA-256, MD5), MAC addresses, color codes (#ff0000), memory addresses, and debugging output. Binary is primarily used in educational contexts, bitwise operation visualization, and low-level hardware documentation.
Conversion Simplicity
Hex is the simplest to convert mentally: each hex digit maps to exactly 4 bits, and two hex digits make one byte. A developer who works with hex regularly can read values like 0x41 as the letter "A" without thinking about it. Base64 conversion requires a lookup table and grouping by 6 bits, which is not something you do in your head. Binary is the most fundamental but the most tedious.
When to Use Base64
Embedding Binary Data in Text Formats
Need to include a small image in a JSON API response? Base64. Need to attach a file to an email programmatically? Base64 (MIME). Need to inline a font in CSS? Base64 data URI. Whenever binary data must travel through a text-only channel, Base64 is the standard choice because it minimizes size overhead.
JWT and Authentication Tokens
JWTs encode their header, payload, and signature as Base64url strings. If you work with authentication systems, you encounter Base64 constantly. Decode a JWT with the JWT Decoder and you will see Base64url in action.
Certificate and Key Storage
PEM files (the ones that start with -----BEGIN CERTIFICATE-----) store Base64-encoded binary data. TLS certificates, SSH keys, and PGP keys all use this format for the same reason: Base64 makes binary data safe to copy, paste, and transmit as text.
When to Use Hexadecimal
Hash Digests and Checksums
When you run sha256sum file.txt, the output is a hex string. Hex is the standard for displaying hash digests because it maps cleanly to bytes, making it easy to compare hashes visually (just look at the first and last few characters). Use the Hash Generator to see hex-formatted hashes of any input.
Color Codes in CSS
Every web developer knows #ff6600. CSS color codes are hex representations of RGB byte values. This is hex encoding applied so naturally that most people do not even think of it as encoding.
Debugging and Low-Level Inspection
Hex dumps are the standard way to inspect binary files, network packets, and memory contents. Tools like xxd, hexdump, and Wireshark all default to hex output because it balances compactness with readability.
When to Use Binary
Learning Bitwise Operations
If you are studying how AND, OR, XOR, and shift operations work, binary representation makes the logic visible. Seeing 1010 AND 1100 = 1000 is clearer than seeing 0xA AND 0xC = 0x8. The Text to Binary converter is a useful learning tool for understanding how characters map to bit patterns.
Networking Protocol Analysis
TCP flags, subnet masks, IP headers — these are defined at the bit level. When you need to understand which specific bits are set in a byte, binary representation is the only format that shows them directly without mental conversion.
Hardware Register Inspection
Embedded systems documentation often describes registers as bit fields. A status register where bit 7 is "ready," bit 6 is "error," and bits 0-5 are a counter makes the most sense when viewed in binary.
Can You Convert Between Them?
Absolutely. All three formats represent the same underlying bytes — just in different text encodings. Converting between them is lossless and instant. The choice of format is purely about how you want to present and transmit the data. Many developer workflows involve converting frequently: receiving a Base64 token, decoding it to examine the bytes in hex, or converting hex color values to check their binary bit patterns.
Free Encoding and Decoding Tools
- Base64 Encoder/Decoder — encode and decode Base64 strings instantly
- Hex Text Converter — convert between text and hexadecimal
- Text to Binary — see the binary representation of any text
- Binary to Text — convert binary strings back to readable text
- ASCII Text Converter — look up ASCII codes and character mappings
- Hash Generator — generate hex-formatted hash digests
Frequently Asked Questions
Is Base64 encryption?
No. Base64 is encoding, not encryption. It does not protect data or require a key. Anyone can decode a Base64 string instantly. It exists to make binary data safe for text transport, not to hide it. If you need to protect data, use an encryption tool.
Why do hex strings always have even length?
Because each byte is represented by exactly two hex characters. A 10-byte value is always 20 hex characters. If you see an odd-length hex string, it usually means a leading zero was dropped — the value 0x0a might be incorrectly displayed as just a instead of 0a.
What is Base64url and how is it different?
Base64url replaces the + character with - and / with _, making the output safe for URLs and filenames. It also typically omits padding (=). JWTs use Base64url specifically because tokens are often passed in URL query parameters.
Can I represent any file as hex?
Yes. Any file is a sequence of bytes, and every byte can be represented as two hex characters. The resulting hex string will be exactly twice the file size. This is how hex editors work — they show you the raw bytes of any file in hex notation.
Why does binary encoding exist if it is so inefficient?
Binary is the native representation of data in computers. Every other encoding (hex, Base64, ASCII) is a human-friendly abstraction on top of binary. Binary encoding as text exists because sometimes you need to see the actual bits — for education, debugging, or understanding low-level protocols where individual bits carry meaning.
Choose Based on Context
Need to ship binary data through a text channel with minimal overhead? Base64. Need to inspect bytes or display a hash digest? Hex. Need to understand individual bits for a learning exercise or hardware project? Binary. The data is the same in all three formats — the encoding just determines how you and your systems interact with it.