basexx

package module
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2023 License: MIT Imports: 6 Imported by: 1

README

Basexx - Convert between digit strings and various number bases

Go Reference Go Report Card Tests Coverage Status

This is basexx, a package for converting numbers to digit strings in various bases and vice versa.

Usage

To get the Base30 encoding of the number 412:

var sb strings.Builder
if err := basexx.EncodeInt64(&sb, 412, basexx.Base30); err != nil { ... }
result := sb.String()

To decode the Base30 digit string "fr":

result, err := basexx.DecodeString("fr", basexx.Base30)

To convert a digit string x in base from to a new digit string in base to:

result, err := basexx.Convert(x, from, to)

To define your own new number base:

// ReverseBase10 uses digits '9' through '0' just to mess with you.
type ReverseBase10 struct{}

func (ReverseBase10) N() int64 { return 10 }

func (ReverseBase10) Digit(val int64) (byte, error) {
  if val < 0 || val > 9 {
    return 0, errors.New("digit value out of range")
  }
  return byte('9' - val), nil
}

func (ReverseBase10) Val(digit byte) (int64, error) {
  if digit < '0’ || digit > '9' {
    return 0, errors.New("invalid encoded digit")
  }
  return int64(9 - (digit - '0’))
}

Documentation

Overview

Package basexx provides functions for converting numbers to and from strings in arbitrary bases.

Index

Examples

Constants

View Source
const (
	// Base2 uses digits "0" and "1"
	Base2 = Alnum(2)

	// Base8 uses digits "0" through "7"
	Base8 = Alnum(8)

	// Base10 uses digits "0" through "9"
	Base10 = Alnum(10)

	// Base12 uses digits "0" through "9" plus "a" and "b"
	Base12 = Alnum(12)

	// Base16 uses digits "0" through "9" plus "a" through "f"
	Base16 = Alnum(16)

	// Base32 uses digits "0" through "9" plus "a" through "v"
	Base32 = Alnum(32)

	// Base36 uses digits "0" through "9" plus "a" through "z"
	Base36 = Alnum(36)
)

Variables

View Source
var (
	// Base30 uses digits 0-9, then lower-case bcdfghjkmnpqrstvwxyz.
	// It excludes vowels (to avoid inadvertently spelling naughty words) and the letter "l".
	// Note, this is not the same as basexx.Alnum(30),
	// which uses 0-9 and then abcdefghijklmnopqrst.
	Base30 = NewTableBase("0123456789bcdfghjkmnpqrstvwxyz")

	// Base50 uses digits 0-9, then lower-case bcdfghjkmnpqrstvwxyz, then upper-case BCDFGHJKMNPQRSTVWXYZ.
	// It excludes vowels (to avoid inadvertently spelling naughty words) plus lower- and upper-case L.
	Base50 = NewTableBase("0123456789bcdfghjkmnpqrstvwxyzBCDFGHJKMNPQRSTVWXYZ")
)
View Source
var Base62 base62

Base62 uses digits 0..9, then a..z, then A..Z.

View Source
var Base94 base94

Base94 uses all printable ASCII characters (33 through 126) as digits.

View Source
var Binary binary

Binary is base 256 encoded the obvious way: digit value X = byte(X).

View Source
var ErrInvalid = errors.New("invalid")

ErrInvalid is returned when a digit or a value is out of range.

Functions

func Convert

func Convert(inp string, from, to Base) (string, error)

Convert converts a string from one base to another.

Example
const base10val = "12345"

// The basexx package has no predefined Base20 type,
// but any base 2 through 36 using alphanumeric digits
// can be defined with basexx.Alnum.
base20 := basexx.Alnum(20)

result, err := basexx.Convert(base10val, basexx.Base10, base20)
if err != nil {
	log.Fatal(err)
}

fmt.Printf("%s (base 10) = %s (base 20)", base10val, result)
Output:

12345 (base 10) = 1ah5 (base 20)

func Decode

func Decode(inp io.Reader, base Base) (*big.Int, error)

Decode decodes an integer expressed as a string in the given base.

func DecodeString

func DecodeString(s string, base Base) (*big.Int, error)

DecodeString decodes a string in the given base.

func Encode

func Encode(out io.Writer, inp *big.Int, base Base) error

Encode encodes inp as a string in the given base. If inp is negative, it is silently made positive.

func EncodeInt64

func EncodeInt64(out io.Writer, inp int64, base Base) error

EncodeInt64 encodes an integer as a string in the given base. If inp is negative, it is silently made positive.

Types

type Alnum

type Alnum int

Alnum is a type for bases from 2 through 36, where the digits for the first 10 digit values are '0' through '9' and the remaining digits are 'a' through 'z'. For decoding, upper-case 'A' through 'Z' are the same as lower-case.

func (Alnum) Digit

func (a Alnum) Digit(val int64) (byte, error)

Digit implements Base.Digit.

func (Alnum) N

func (a Alnum) N() int64

N implements Base.N.

func (Alnum) Val

func (a Alnum) Val(digit byte) (int64, error)

Val implements Base.Val.

type Base

type Base interface {
	// N is the number of digits in the base.
	// It must be at least 2 and at most 256.
	N() int64

	// Val is the value of the given digit in this base.
	Val(byte) (int64, error)

	// Digit is the digit with the given value in this base.
	Digit(int64) (byte, error)
}

Base is the abstract type of a number base.

type TableBase

type TableBase struct {
	// contains filtered or unexported fields
}

TableBase is a Base initialized from a string of digits.

func NewTableBase

func NewTableBase(digits string) TableBase

NewTableBase returns a new TableBase initialized from the given digits.

func (TableBase) Digit

func (b TableBase) Digit(val int64) (byte, error)

Digit implements Base.Digit.

func (TableBase) N

func (b TableBase) N() int64

N implements Base.N.

func (TableBase) Val

func (b TableBase) Val(digit byte) (int64, error)

Val implements Base.Val.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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