revhex

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 13, 2025 License: MIT Imports: 4 Imported by: 0

README

revhex ⏪

PkgGoDev

Go module that implements reverse hexadecimal encoding and decoding, as used in the Jujutsu version control system.

The reverse hex format is normal 16-base hexadecimal encoding but using z-k as the alphabet instead of 0-9a-f.

func Example() {
	sample := []byte{187, 132, 192, 163, 222, 186, 197, 248}
	fmt.Println("hex:    ", hex.EncodeToString(sample))
	fmt.Println("revhex: ", revhex.EncodeToString(sample))

	// Output:
	// hex:     bb84c0a3debac5f8
	// revhex:  oorvnzpwmlopnukr
}

The implementations in this package are modeled after the standard library encoding/hex package, and have identical APIs and performance characteristics.

Documentation

Overview

Package revhex implements reverse hexadecimal encoding and decoding.

The reverse hex format is just normal 16-base hexadecimal encoding but using `z-k` as the alphabet instead of `0-9a-f`.

The implementations in this package are closely modeled after the standard library encoding/hex package, and have identical APIs and performance characteristics.

Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/mroth/revhex"
)

func main() {
	sample := []byte{187, 132, 192, 163, 222, 186, 197, 248}
	fmt.Println("bytes:  ", sample)
	fmt.Println("hex:    ", hex.EncodeToString(sample))
	fmt.Println("revhex: ", revhex.EncodeToString(sample))

}
Output:

bytes:   [187 132 192 163 222 186 197 248]
hex:     bb84c0a3debac5f8
revhex:  oorvnzpwmlopnukr

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrLength = errors.New("encoding/revhex: odd length reverse hex string")

ErrLength reports an attempt to decode an odd-length input using Decode or DecodeString. The stream-based Decoder returns io.ErrUnexpectedEOF instead of ErrLength.

Functions

func AppendDecode

func AppendDecode(dst, src []byte) ([]byte, error)

AppendDecode appends the reverse hexadecimally decoded src to dst and returns the extended buffer. If the input is malformed, it returns the partially decoded src and an error.

func AppendEncode

func AppendEncode(dst, src []byte) []byte

AppendEncode appends the reverse hexadecimally encoded src to dst and returns the extended buffer.

func Decode

func Decode(dst, src []byte) (int, error)

Decode decodes src into DecodedLen(len(src)) bytes, returning the actual number of bytes written to dst.

Decode expects that src contains only reverse hexadecimal characters and that src has even length. If the input is malformed, Decode returns the number of bytes decoded before the error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/mroth/revhex"
)

func main() {
	src := []byte("vrtutntntkxzvstksztrtusxxy")

	dst := make([]byte, revhex.DecodedLen(len(src)))
	n, err := revhex.Decode(dst, src)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", dst[:n])

}
Output:

Hello Gopher!

func DecodeString

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

DecodeString returns the bytes represented by the reverse hexadecimal string s.

DecodeString expects that src contains only reverse hexadecimal characters and that src has even length. If the input is malformed, DecodeString returns the bytes decoded before the error.

Example
package main

import (
	"fmt"
	"log"

	"github.com/mroth/revhex"
)

func main() {
	const s = "vrtutntntkxzvstksztrtusxxy"
	decoded, err := revhex.DecodeString(s)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s\n", decoded)

}
Output:

Hello Gopher!

func DecodedLen

func DecodedLen(x int) int

DecodedLen returns the length of a decoding of x source bytes. Specifically, it returns x / 2.

func Encode

func Encode(dst, src []byte) int

Encode encodes src into EncodedLen(len(src)) bytes of dst. As a convenience, it returns the number of bytes written to dst, but this value is always EncodedLen(len(src)). Encode implements reverse hexadecimal encoding.

Example
package main

import (
	"fmt"

	"github.com/mroth/revhex"
)

func main() {
	src := []byte("Hello Gopher!")

	dst := make([]byte, revhex.EncodedLen(len(src)))
	revhex.Encode(dst, src)

	fmt.Printf("%s\n", dst)

}
Output:

vrtutntntkxzvstksztrtusxxy

func EncodeToString

func EncodeToString(src []byte) string

EncodeToString returns the reverse hexadecimal encoding of src.

Example
package main

import (
	"fmt"

	"github.com/mroth/revhex"
)

func main() {
	src := []byte("Hello Gopher!")
	encodedStr := revhex.EncodeToString(src)

	fmt.Printf("%s\n", encodedStr)

}
Output:

vrtutntntkxzvstksztrtusxxy

func EncodedLen

func EncodedLen(n int) int

EncodedLen returns the length of an encoding of n source bytes. Specifically, it returns n * 2.

func NewDecoder

func NewDecoder(r io.Reader) io.Reader

NewDecoder returns an io.Reader that decodes reverse hexadecimal characters from r. NewDecoder expects that r contain only an even number of reverse hexadecimal characters.

func NewEncoder

func NewEncoder(w io.Writer) io.Writer

NewEncoder returns an io.Writer that writes lowercase reverse hexadecimal characters to w.

Types

type InvalidByteError

type InvalidByteError byte

InvalidByteError values describe errors resulting from an invalid byte in a reverse hex string.

func (InvalidByteError) Error

func (e InvalidByteError) Error() string

Jump to

Keyboard shortcuts

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