numconv

numconv is a library that converts strings to numbers and numbers to strings using default or user supplied encoding alphabets, written by Gustavo Picón and licensed under the Apache License 2.0.

Installation

numconv has been tested in Python 2.4, 2.5, 2.6, 2.7, 3.0 and 3.1. Other versions may work but are not supported.

You have several ways to install numconv. If you’re not sure, just use pip

pip (or easy_install)

You can install the release versions from numconv’s PyPI page using pip:

pip install numconv

or if for some reason you can’t use pip, you can try easy_install:

easy_install --always-unzip numconv

setup.py

Download a release from the numconv download page and unpack it, then run:

python setup.py install

API

class numconv.NumConv(radix=10, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~')

Class to create converter objects.

Parameters:
  • radix – The base that will be used in the conversions. The default value is 10 for decimal conversions.
  • alphabet

    A string that will be used as a encoding alphabet. The length of the alphabet can be longer than the radix. In this case the alphabet will be internally truncated.

    The default value is numconv.BASE85

Raises:
  • TypeError – when radix isn’t an integer
  • ValueError – when radix is invalid
  • ValueError – when alphabet has duplicated characters
int2str(num)

Converts an integer into a string.

Parameters:
  • num – A numeric value to be converted to another base as a string.
Return type:

string

Raises:
  • TypeError – when num isn’t an integer
  • ValueError – when num isn’t positive

Examples (taken from tests.py):

3735928559 to hexadecimal:

>> NumConv(16).int2str(3735928559)
'DEADBEEF'

19284 to binary:

>> NumConv(2).int2str(19284)
'100101101010100'

37 to base 4 using a custom dictionary:

>> NumConv(4, 'rofl').int2str(37)
'foo'

Very large number to BASE85:

>> NumConv(85).int2str(2693233728041137)
'~123AFz@'
str2int(num)

Converts a string into an integer.

If possible, the built-in python conversion will be used for speed purposes.

Parameters:
  • num – A string that will be converted to an integer.
Return type:

integer

Raises ValueError:
 

when num is invalid

Examples (taken from tests.py):

Hexadecimal ‘DEADBEEF’ to integer:

>> NumConv(16).str2int('DEADBEEF')
3735928559

Binary ‘100101101010100’ to integer:

>> NumConv(2).str2int('100101101010100')
19284

Base 4 with custom encoding ‘foo’ to integer:

>> NumConv(4, 'rofl').str2int('foo')
37

BASE85 ‘~123AFz@’ to integer:

>> NumConv(85).str2int('~123AFz@')
2693233728041137
numconv.BASE85
Alphabet defined in section 4 of RFC 1924. Supposed to be a joke (it is an April’s fools RFC after all), but is quite useful because it can be used as a base for the most common numeric conversions.
numconv.BASE16
numconv.BASE32
numconv.BASE32HEX
numconv.BASE64
numconv.BASE64URL
Alphabets defined in RFC 4648. Not really for common numeric conversion use.
numconv.BASE62
Useful for URL shorteners.
numconv.int2str(num, radix=10, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~')
helper function for quick base conversions from integers to strings
numconv.str2int(num, radix=10, alphabet='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~')
helper function for quick base conversions from strings to integers

Indices and tables

Table Of Contents

This Page