Documentation ¶
Overview ¶
Package basexx provides functions for converting numbers to and from strings in arbitrary bases.
Index ¶
- Constants
- Variables
- func Convert(inp string, from, to Base) (string, error)
- func Decode(inp io.Reader, base Base) (*big.Int, error)
- func DecodeString(s string, base Base) (*big.Int, error)
- func Encode(out io.Writer, inp *big.Int, base Base) error
- func EncodeInt64(out io.Writer, inp int64, base Base) error
- type Alnum
- type Base
- type TableBase
Examples ¶
Constants ¶
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 ¶
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") )
var Base62 base62
Base62 uses digits 0..9, then a..z, then A..Z.
var Base94 base94
Base94 uses all printable ASCII characters (33 through 126) as digits.
var Binary binary
Binary is base 256 encoded the obvious way: digit value X = byte(X).
var ErrInvalid = errors.New("invalid")
ErrInvalid is returned when a digit or a value is out of range.
Functions ¶
func Convert ¶
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 DecodeString ¶
DecodeString decodes a string in the given base.
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.
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.