CodeToolProCodeToolPro
GitHub

Base62 Encoder / Decoder

Input

  • Output

  • 技术详情

    Base62 编码/解码器的工作原理

    工具功能

    Base62 编码/解码器使用 0-9(数字)、A-Z(大写字母)和 a-z(小写字母)共 62 个字符对数据进行编码。Base62 的字符集是人类可读且 URL 安全的——不需要任何转义或编码即可直接在 URL 查询参数和路径中使用。工具支持将任意字节数据编码为 Base62 字符串,以及反向解码。由于 62 个字符刚好覆盖了所有数字和字母,编码密度比 Base64 略低(约膨胀 34%),但换来的是完全的 URL-Safe 特性。


    常见开发者使用场景

    Base62 在短链接服务中是最常用的编码方案。像 bit.ly、TinyURL 这样的短链接服务使用 Base62 将长整数 ID 编码为短字符串(如 /abc123)。在数据库 ID 对外暴露时,用 Base62 编码可以模糊化原始 ID 并缩短长度。API 网关使用 Base62 生成更短的 token 或 session ID。YouTube 的视频 ID(如 dQw4w9WgXcQ)使用了类似 Base62 的编码方案。文件存储系统使用 Base62 生成唯一文件标识符。

    配合 Base64 编码器 可以对比编码密度差异,或使用 UUID 生成器 先生成 UUID 再用 Base62 缩短长度。


    Base62 编码的数学原理

    Base62 编码本质上是大整数进制转换:

    • 编码过程:将字节数组视为一个大整数 N,反复计算 N mod 62 取余数,余数索引映射到字符集,然后将 N 更新为 N / 62,直到 N 为 0。
    • 解码过程:对 Base62 字符串的每个字符查找其索引值,乘以 62 的对应幂次,求和得到原始整数,再转换回字节数组。
    • 特殊处理:原始数据中的前导零字节(0x00)需特殊处理——每个零字节在编码结果中对应字符 '0'。
    • 字符集顺序:常见顺序为 0-9 A-Z a-z,但也有变体使用不同排列。编解码时必须使用一致的顺序。

    常见陷阱与注意事项

    • 大数计算:对于超过 64 位的数值,需要大整数库支持(如 JavaScript 的 BigInt)。Base62 编解码本质上是大整数运算。
    • 前导零:原始字节中的前导 0x00 编码后变为 '0' 字符,解码时前导 '0' 字符需要还原为 0x00 字节。
    • 字符集变体:GMP 库中的 Base62 使用 '0-9A-Za-z' 顺序,而 PHP 的 gmp_strval 行为不同。跨平台交互时需确认字符集。
    • 不适合二进制:Base62 编码二进制大文件时性能较差(大整数运算开销大),对于二进制数据优先使用 Base64 或 Base32。

    何时使用此工具而非代码

    在生成短链接路径、编码数据库 ID 为短字符串、或需要 URL 安全的紧凑编码时使用此工具。生产环境中推荐使用 base-x 库(NPM)或 Python 的 base62 包,它们通过优化的大整数算法处理任意长度输入。

    How to Use Base62 Encoder / Decoder

    1. 1Enter your text or data into the input field
    2. 2Select the encoding or decoding direction
    3. 3View the encoded or decoded result instantly
    4. 4Copy the output for use in your application

    Frequently Asked Questions

    • Is the encoding/decoding process reversible?

      Yes. All encoding formats supported here are lossless and fully reversible. Decode back to the original data at any time with 100% fidelity.

    • Is Base62 Encoder / Decoder free?

      Yes, 100% free. No registration, no credit card required. All tools are freely available for developers worldwide.

    • Does this tool upload my data to any server?

      No. All processing runs locally in your browser using client-side JavaScript. Your input data, code, or files never leave your device. We do not store, log, or transmit any user data.