ข้ามไปยังเนื้อหาหลัก
FreeOnlineTools Go
ไทย
comparison

Base64 เทียบกับการเข้ารหัส Hex: ควรเลือกแบบไหน?

By · Updated 2026-09-02

Quick Answer

ใช้ Base64 เมื่อขนาดสำคัญ (ไฟล์แนบ email, data URLs, API payloads) ใช้ Hex เมื่อความอ่านง่ายสำคัญ (debugging, แสดง hash, รหัสสี, ที่อยู่หน่วยความจำ) Base64 กระชับกว่า; Hex ตรวจสอบได้ง่ายกว่า

Introduction

Base64 และ Hex (เลขฐานสิบหก) เป็นรูปแบบการเข้ารหัส binary-to-text ทั้งคู่ Base64 เข้ารหัส 3 bytes เป็น 4 อักขระ ASCII (33% overhead) Hex เข้ารหัส 1 byte เป็น 2 อักขระ ASCII (100% overhead) ทั้งคู่ใช้แสดงข้อมูล binary ในบริบทข้อความ แต่ต่างกันมากในด้านประสิทธิภาพและความอ่านง่าย

Step by Step

  1. Compare encoding efficiency

    Base64: 3 bytes → 4 characters = 33% overhead. Hex: 1 byte → 2 characters = 100% overhead. For 1 KB of data, Base64 produces ~1.33 KB; Hex produces 2 KB. Base64 is 50% smaller than Hex for the same binary data.

  2. Compare readability

    Hex uses 16 characters (0-9, A-F), each representing 4 bits. You can easily read byte values. Base64 uses 64 characters (A-Z, a-z, 0-9, +, /), representing 6 bits each. Base64 output is harder to inspect manually because bytes are split across character boundaries.

  3. Compare use cases

    Base64: email attachments (MIME), data URLs, JWT payloads, API binary data transfer. Hex: hash digests (MD5, SHA-256), color codes (#FF5733), memory addresses, MAC addresses, binary debugging.

  4. Compare character set and safety

    Base64 uses + and / which are not URL-safe. Hex uses only 0-9 and A-F/a-f — always URL-safe and filename-safe. Hex is safe in any context; Base64 may need URL-safe variant.

  5. Decide based on your use case

    Email, data URLs, JWT, API payloads: Base64 (size matters). Hash display, debugging, color codes, memory addresses: Hex (readability matters).

Examples

Same data in Base64 and Hex

Input: Input: 'Hello' (5 bytes) Base64: SGVsbG8= (8 chars) Hex: 48656c6c6f (10 chars)

Output: Base64 is 20% smaller. Hex is easier to inspect (each pair = 1 byte).

Common Problems

  • Base64 is not URL-safe: standard Base64 uses + and / which break in URLs and filenames — use URL-safe Base64 (- and _) for web contexts.
  • Hex has 100% size overhead: Hex doubles the data size (1 byte → 2 chars), which is wasteful for large binary transfers — use Base64 when size matters.
  • Base64 padding ambiguity: Base64 appends = padding characters which some systems strip or reject — handle padding consistently on both encode and decode sides.
  • Hex case inconsistency: some systems use uppercase (A-F), others lowercase (a-f) — normalize case before comparison to avoid false mismatches.

Tips

  • Use Base64 for email attachments, data URLs, and API payloads where size matters — it has only 33% overhead vs Hex's 100%.
  • Use Hex for hash digests, color codes, MAC addresses, and debugging where human readability matters — each byte is a visible 2-character pair.
  • Use URL-safe Base64 (replace + with -, / with _, strip =) when encoding binary data in URLs, query parameters, or filenames.
  • When converting between Base64 and Hex, always decode to raw binary first, then re-encode — never try to convert character-by-character.

Related Tools

Related Guides

References