Ascii85(Base85)エンコーディングを使う方法
Base85 方式である Ascii85 を使ってバイナリデータをエンコードします。用途によっては Base64 よりコンパクトになります。
问题
バイナリデータのテキスト表現として Base64 よりコンパクトなものが必要で、対象システムがより大きな文字セットに対応している状況です。
前提条件
- Base64 の理解
- バイナリデータへの習熟
ステップ
-
1. Understand the 5-to-4 ratio
Ascii85 encodes 4 bytes of binary into 5 ASCII characters, using an 85-character alphabet. This is more compact than Base64's 3-to-4 ratio, saving about 7% space.
入力4 bytes → 5 chars输出vs Base64: 3 bytes → 4 chars -
2. Encode the data
Split the input into 4-byte groups, interpret each as a 32-bit big-endian integer, and convert it to base 85 using 5 digits. Map each digit to a character starting from '!' (ASCII 33). Handle the remainder if the input length is not a multiple of 4.
入力test输出5EWac -
3. Decode and verify
Reverse the process: convert each 5-character group back to a 32-bit integer, then split into 4 bytes. Strip the wrapper delimiters (<~ and ~> for Adobe Ascii85) before decoding if present.
入力5EWac输出test
期待される結果
元のバイナリデータに正確にデコードできる、コンパクトな ASCII 文字列。
よくあるエラー
- Mixing Adobe Ascii85 and standard Ascii85
- Adobe Ascii85 wraps the output in <~ and ~> and uses a special 'z' shorthand for all-zero groups. Know which variant your decoder expects.
- Incorrect remainder handling
- When the input is not a multiple of 4, pad with zero bytes, encode, then truncate the output to the correct length. The decoder must reverse this.
トラブルシューティング指引
- If the decoded data is wrong, check for the Adobe 'z' shorthand which represents 4 zero bytes as a single character.
- Verify the alphabet starts at '!' (ASCII 33), not at '0' or 'A'.
セキュリティ上の制限
- Ascii85 is a reversible encoding with no security properties.