Ascii85 Encode And Decode
Pack binary bytes into printable Ascii85 text with encoding/ascii85, then decode the result and confirm a byte-exact round trip.
Code
package main
import (
"encoding/ascii85"
"fmt"
)
func main() {
payload := []byte("binary payload: 0x00 0x01 0x02 0xff")
// MaxEncodedLen 给出目标缓冲区的安全上界
dst := make([]byte, ascii85.MaxEncodedLen(len(payload)))
n := ascii85.Encode(dst, payload)
fmt.Println("encoded:", string(dst[:n]))
fmt.Println("encoded bytes:", n)
// Decode 返回写入的字节数;空白字符由调用方自行处理
decoded := make([]byte, len(payload))
ndst, nsrc, err := ascii85.Decode(decoded, dst[:n], true)
if err != nil {
fmt.Println("decode failed:", err)
return
}
fmt.Println("decoded bytes:", ndst, "consumed:", nsrc)
fmt.Println("roundtrip:", string(decoded[:ndst]) == string(payload))
}Under the hood
The compact reading: all the work lives in the function main, doing all the work.
err is inspected at 1 call site before any result is trusted.
Caveats
- The language toolchain itself is the one real prerequisite.
Applying it
The main signature reads func main()
Each run prints the 5 labeled blocks encoded:, encoded bytes:, decode failed:, decoded bytes:, roundtrip: in source order.
Statements that matter
if err != nil {
fmt.Println("decode failed:", err)
return
}
dst := make([]byte, ascii85.MaxEncodedLen(len(payload)))
decoded := make([]byte, len(payload))
Interactive Tools for This Topic
ASCII85 EncoderEncode and decode ASCII85 (Base85), the Adobe variant with z zero-compression, handling arbitrary bytes and Unicode.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.Base32 Encoder & DecoderEncode and decode RFC 4648 Base32 with padding, fully UTF-8 safe so Unicode text round-trips without corruption. Nothing leaves your browser.Base45 Encoder and DecoderEncode and decode text with the Base45 alphabet defined in RFC 9285, with UTF-8 support, padding-free output, and clear validation errors.
FAQ
- What does Ascii85 Encode And Decode print when run?
- Execute the whole file; each printed line is prefixed with a label like `encoded:`, `encoded bytes:`, `decode failed:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.