base62

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

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

Go to latest
Published: Apr 8, 2015 License: MIT Imports: 5 Imported by: 23

README

base62

Build Status GoDoc

base62 implements base62 encoding for integer numbers of arbitrary precision.

Currently both int64 and *big.Int are supported

Encoding

The encoding is different to base64 encoding in two ways:

  • Reduced character set, without symbols - 0-9A-Za-z
  • Character ordering - base64 is A-Za-z0-9

Note that this character ordering is also different to most other base62 encodings, but preserves the sort order of encoded values.

As a result, the encoded strings can be lexically sorted if either a sorting order such as shortlex is used, or if the resulting strings are left padded to ensure a consistent length.

Usage

First go get the package:

go get github.com/mattheath/base62

This can then be used as below:

package main

import (
    "fmt"
    "math/big"
    "github.com/mattheath/base62"
)

func main() {
	// Encoding 64 bit integers
	var n int64 = 4815162342
    encoded := base62.EncodeInt64(n)
    fmt.Println(encoded) // prints 5Frvgk

    // Arbitrary precision integers can be specified using the math/big pkg
    var b *big.Int = new(big.Int)
    b.SetString("340282366920938463463374607431768211455") // 128bit unsigned int
    bigEncoded := base62.EncodeBigInt(b)
    fmt.Println(bigEncoded) // prints 7n42DGM5Tflk9n8mt7Fhc7

    // Padding can be specified using an option on an Encoding
    // eg. to pad strings to a minimum length of 15 chars:
    e := base62.NewStdEncoding().Option(Padding(15))
    encoded = e.EncodeInt64(n)
    fmt.Println(encoded) // prints 0000000005Frvgk
}

Documentation

Overview

Package base62 implements base62 encoding

Index

Constants

This section is empty.

Variables

View Source
var StdEncoding = NewStdEncoding()

StdEncoding is the standard base62 encoding

Functions

func DecodeToBigInt

func DecodeToBigInt(s string) *big.Int

DecodeToBigInt returns an arbitrary precision integer from the base62 encoded string using the StdEncoding

func DecodeToInt64

func DecodeToInt64(s string) int64

DecodeToInt64 decodes a base62 encoded string using the StdEncoding

func EncodeBigInt

func EncodeBigInt(n *big.Int) string

EncodeBigInt returns the base62 encoding of an arbitrary precision integer using the StdEncoding

func EncodeInt64

func EncodeInt64(n int64) string

EncodeInt64 returns the base62 encoding of n using the StdEncoding

func Padding

func Padding(n int) option

Padding sets the minimum string length returned when encoding strings shorter than this will be left padded with zeros

Types

type Encoding

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

func NewEncoding

func NewEncoding(encoder string) *Encoding

NewEncoding returns a new Encoding defined by the given alphabet

func NewStdEncoding

func NewStdEncoding() *Encoding

NewStdEncoding returns an Encoding preconfigured with the standard base62 alphabet

func (*Encoding) DecodeToBigInt

func (e *Encoding) DecodeToBigInt(s string) *big.Int

DecodeToBigInt returns an arbitrary precision integer from the base62 encoded string

func (*Encoding) DecodeToInt64

func (e *Encoding) DecodeToInt64(s string) int64

DecodeToInt64 decodes a base62 encoded string

func (*Encoding) EncodeBigInt

func (e *Encoding) EncodeBigInt(n *big.Int) string

EncodeBigInt returns the base62 encoding of an arbitrary precision integer

func (*Encoding) EncodeInt64

func (e *Encoding) EncodeInt64(n int64) string

EncodeInt64 returns the base62 encoding of n

func (*Encoding) Option

func (e *Encoding) Option(opts ...option) *Encoding

Option sets a number of optional parameters on the encoding

Jump to

Keyboard shortcuts

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