Git Product home page Git Product logo

base62's Introduction

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
}

base62's People

Contributors

mattheath avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.