synopsys: | Python library to convert strings to numbers and numbers to strings. |
---|---|
copyright: | 2008-2009 by Gustavo Picon |
license: | Apache License 2.0 |
version: | 2.0 |
url: | http://code.tabo.pe/numconv/ |
documentation: | numconv-docs |
examples: | numconv-tests |
numconv converts a string into a number and a number into a string using default or user supplied encoding alphabets.
Bases: object
Class to create converter objects.
Parameters: |
|
---|---|
Raises TypeError: | |
when radix isn’t an integer |
|
Raises ValueError: | |
when radix is invalid |
|
Raises ValueError: | |
when alphabet has duplicated characters |
Converts an integer into a string.
Parameter: | num – A numeric value to be converted to another base as a string. |
---|---|
Return type: | string |
Raises TypeError: | |
when num isn’t an integer | |
Raises 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(2693233728041137L) '~123AFz@'
Converts a string into an integer.
If possible, the built-in python conversion will be used for speed purposes.
Parameter: | 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') 3735928559LBinary ‘100101101010100’ to integer:
>> NumConv(2).str2int('100101101010100') 19284Base 4 with custom encoding ‘foo’ to integer:
>> NumConv(4, 'rofl').str2int('foo') 37BASE85 ‘~123AFz@’ to integer:
>> NumConv(85).str2int('~123AFz@') 2693233728041137L