Base64 与 Hex 编码:什么时候用哪一个
两者的字节语义、长度差、URL 安全性与可读性对比,配工具直达与选型结论。
Base64 and hex are both ways to show binary bytes as printable text, yet they get chosen for very different jobs. Picking the wrong one costs you bandwidth, log readability or debugging time. Here is the practical difference.
The size question
Hex maps every byte to exactly two characters, so output is always twice the input size. Base64 packs 3 bytes into 4 characters, which is about 33 percent overhead. When encoding kilobytes of payload into a URL or a JSON field, that difference adds up fast.
The readability question
Hex wins when humans inspect data: every two characters are one byte, offsets align, and byte patterns are easy to spot in logs or git binaries. Base64 wins when machines transport data, because the payload is shorter and URL-safe variants exist.
Where each one shows up daily
- JWTs use URL-safe Base64 for header, payload and signature — decode any token with the JWT Decoder.
- Data URLs embed small images as base64 directly into HTML or CSS.
- Commit diffs and checksum output print bytes as hex — inspect any string the same way with the Hex to Text Converter.
- Email (MIME) standardized Base64 for attachments decades ago.
Quick decision table
| Situation | Pick |
|---|---|
| Embed binary inside JSON, XML or a URL | Base64 (URL-safe for URLs) |
| Human wants byte-level inspection | Hex |
| Bandwidth matters, text channel required | Base64 |
| Comparing hashes or binary fingerprints | Hex |
Try both sides live: the Base64 Encoder & Decoder and the Hex Converter run entirely in your browser — handy when a UTF-8 multibyte character makes both outputs confusing at a glance.
相关指南
文中提到的工具
Base64 Encoder / Decoder
Encode text to Base64 or decode it back in one click, plus image ⇄ Base64 data-URL conversion with drag & drop. Free, instant and 100% browser-based — nothing is uploaded.
Open tool →Hex to Text Converter
Convert text to hexadecimal (UTF-8) and decode hex strings back to readable text. Tolerates 0x prefixes, colons and spaces — with strict position-aware error messages.
Open tool →Unicode Converter
Convert any text to \uXXXX JavaScript escapes, U+XXXX code-point lists, 0x hex or numeric entities — and paste escapes back to readable text. Emoji and surrogate pairs handled correctly.
Open tool →