Documentation ¶
Overview ¶
Package basexx permits converting between digit strings of arbitrary bases.
Index ¶
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 base30
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.
var Base50 base50
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.
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 used for invalid input to Base.Encode and Base.Decode.
Functions ¶
func Convert ¶
Convert converts the digits of src, writing them to dest. Both src and dest specify their bases. Return value is the number of digits written to dest (even in case of error). This function consumes all of src before producing any of dest, so it may not be suitable for input streams of arbitrary length.
Example ¶
package main import ( "fmt" "log" "github.com/bobg/basexx" ) func main() { 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) // A Buffer can serve as a Source for Convert. src := basexx.NewBuffer([]byte(base10val), basexx.Base10) // Allocate enough space (according to basexx.Length) for holding the result. destBuf := make([]byte, basexx.Length(10, 20, len(base10val))) // A Buffer can also serve as a Dest for Convert. dest := basexx.NewBuffer(destBuf[:], base20) _, err := basexx.Convert(dest, src) if err != nil { log.Fatal(err) } // Use Written to get the written-to portion of the allocated byte slice (destBuf). result := dest.Written() fmt.Printf("%s (base 10) = %s (base 20)", base10val, string(result)) }
Output: 12345 (base 10) = 1ah5 (base 20)
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 the base, // i.e. the number of unique digits. // Behavior is undefined if the value of N() varies during the lifetime of a Base // or if N() < 2. N() int64 // Encode converts a digit value to the byte representing its digit. // The input must be a valid digit value between 0 and N()-1, inclusive. Encode(int64) (byte, error) // Decode converts an encoded digit byte into its numeric value. Decode(byte) (int64, error) }
Base is the type of a base.
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer can act as a Source or a Dest (but not both at the same time) in the case where each byte in a given slice encodes a single digit in the desired base. The digits in the buffer are in the expected order: namely, most-significant first, least-significant last.
type Dest ¶
type Dest interface { // Prepend encodes the next-most-significant digit value and prepends it to the destination. Prepend(int64) error // Base gives the base of the Dest. // Digit values in the Dest must all be between 0 and Base()-1, inclusive. // Behavior is undefined if the value of Base() varies during the lifetime of a Dest // or if Base() < 2. Base() int64 }
Dest is a destination for writing digits in a given base. Digits are written right-to-left, from least significant to most.
type Source ¶
type Source interface { // Read produces the value of the next-least-significant digit in the source. // The value must be between 0 and Base()-1, inclusive. // End of input is signaled with the error io.EOF. Read() (int64, error) // Base gives the base of the Source. // Digit values in the Source must all be between 0 and Base()-1, inclusive. // Behavior is undefined if the value of Base() varies during the lifetime of a Source // or if Base() < 2. Base() int64 }
Source is a source of digit values in a given base.