Git Product home page Git Product logo

bases.py's Introduction

Build Status

bases.py

A python port for https://github.com/aseemk/bases.js

Utility for converting numbers to/from different bases/alphabets. Common bases have convenience aliases (see below), but arbitrary/custom alphabets can be used.

As most people have noticed pythons int() only supports one way base conversions. int('lol',36) // => 28101. Pythons format() also only supports conversion the other way up to base 16. This library was created to help those python programmers by providing a bases conversion system that works both ways, can use customized conversions and supports from base 2 all the way up to base 64 all in one library.

Installation

Python:

pip install bases.py

Usage

from bases import Bases
bases = Bases()

bases.toBase16(200)                // => 'c8'
bases.toBase62(99999)              // => 'q0T'
bases.toAlphabet(300, 'aAbBcC')    // => 'Abba'

bases.fromBase16('c8')               // => 200
bases.fromBase62('q0T')              // => 99999
bases.fromAlphabet('Abba', 'aAbBcC') // => 300

API

Going from numbers to strings:

  • toAlphabet(num, alphabet): returns a string representation of the given number for the given alphabet, where the alphabet is an arbitrary string of characters. (See known alphabets below for examples.)

  • toBase(num, base): convenience helper for known bases (see below).

  • toBaseX(num): convenience helpers for known bases (see below), e.g. toBase62(num).

Going from strings to numbers:

  • fromAlphabet(str, alphabet): returns an integer representation of the given string for the given alphabet.

  • fromBase(num, base): convenience helper for known bases.

  • fromBaseX(str): convenience helpers for known bases.

Known Bases/Alphabets

Numbers only:

Base-2 01
... 012...
Base-10 0123456789

Letters only:

Base-26 abcdefghijklmnopqrstuvwxyz
Base-52 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Alphanumeric:

Base-11 0123456789a
... 0123456789ab...
Base-16 0123456789abcdef
Base-36 0123456789abcdefghijklmnopqrstuvwxyz
Base-62 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

Human-friendly:

Base-32 (from Douglas Crockford) 0123456789ABCDEFGHJKMNPQRSTVWXYZ (no IOLU)
Base-58 (from Flickr) 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ (no 0lIO)

Other:

Base-64 (as standardized) ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

Base-64 warning: besides there being several different standards, padding isn't currently added and line lengths aren't tracked. Not recommended for use with APIs that expect formal base-64 strings!

License

MIT license. (c) 2015-2016 Belldandu and contributors.

bases.py's People

Contributors

belldandu avatar borisovcode avatar sorch avatar

Watchers

 avatar  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.