asciihex

package module
v0.0.0-...-b37c202 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2025 License: 0BSD Imports: 5 Imported by: 0

README

asciihex

Go Reference

asciihex is a simple and expressive Go library for encoding and decoding strings or byte slices into a human-friendly, roundtrip-safe format.

It strikes a careful balance between readability and precision — making it ideal for logs, debugging, and systems introspection.

✨ Features

The syntax is designed to be both human-readable and safe to work with:

  • Prints printable ASCII characters as-is
  • Encodes control characters using caret (^) notation (^@, ^A, ..., ^?)
  • Encodes non-ASCII bytes using hex escape (~8F)
  • Escapes special characters:
    • ~~~
    • ~^^
  • Fully reversible and roundtrip-safe

The package interface is limited to two functions:

  • Encode(data []byte) string
    Encodes a byte slice into a string
  • Decode(data string) ([]byte, error)
    Decodes a string back into a byte slice

📦 Installation

No third-party dependencies. Drop the package into your project:

go get github.com/0x5a17ed/asciihex

🚀 Quick Start

data := []byte{'H', 'e', 'l', 'l', 'o', 0x0A, 0x1E, 0x8F, '~', '^'}
encoded := asciihex.Encode(data)
// encoded == "Hello^J^^~8F~~~^"

decoded, err := asciihex.Decode(encoded)
// decoded == original data

🌱 Motivation

This package was built with care to provide a log-safe, human-legible format for debugging binary data. Whether you're inspecting packet payloads, serial protocols, or log traces, asciihex gives you a clean, reversible format that won’t make your eyes bleed.

📜 License

This project is licensed under the 0BSD Licence — see the LICENCE file for details.

🥇 Acknowledgments

The design and the implementation are roughly based on the idea and syntax of the caret notation binary data representation.


Made with ❤️ for data you want to understand

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(s string) ([]byte, error)

Decode decodes a string into a byte slice using the specified encoding rules.

func Encode

func Encode(data []byte) string

Encode converts binary data into a readable string representation using the following rules:

  • Printable ASCII characters (0x20-0x7E) except '~' and '^' are kept as-is
  • Control characters (0x00-0x1F) are encoded as '^' followed by their corresponding control picture character
  • The DEL character (0x7F) is encoded as "^?"
  • The '~' character is encoded as "~~"
  • The '^' character is encoded as "~^"
  • All other bytes (>= 0x80) are encoded as "~" followed by their two-digit hexadecimal representation

This encoding ensures that binary data can be safely represented as printable ASCII text, similar to quoted-printable or ASCII armor encodings.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL