Convert numbers of any base back and forth.
Project description
basencode
Convert numbers of any base back and forth.
Getting Started
To install basencode
, run python3 -m pip install basencode
. Now you should be able to use basencode
in any Python program.
Example
>>> from basencode import *
>>> myint1 = Integer(12345)
>>> myint1.to_base(64) # Default digits are used, but can be overridden
'30v'
>>> myint1.to_base(8)
'30071'
>>> myint1.to_octal()
'30071'
>>> myint1.to_bin()
'11000000111001'
>>> myint1.to_base(2, list('-+')) # Default digits are overridden
'++------+++--+'
>>> myint1.to_base(33)
'BB3'
>>> from string import ascii_lowercase, ascii_uppercase, digits, punctuation
>>> myint1.to_base(76, list(digits + ascii_lowercase + ascii_uppercase + punctuation[:14]))
'2ax'
>>> myint1.to_base(76) # Digits provided above were stored as default digits for base 76
'2ax'
>>> myint2 = Integer(54321)
>>> myint1 + myint2
Integer(66666)
>>> myint2 - myint1
Integer(41976)
>>>
>>> myint1 * myint2
Integer(670592745)
>>> myint2 / myint1
Integer(4)
>>> myint2 // myint1
Integer(4)
>>> myint2 % myint1
Integer(4941)
>>> divmod(myint2, myint1)
(Integer(4), Integer(4941))
TODO
- Support operations between
int
andInteger
- Add support for
float
s - Retain all default digits during arithmetic operations
Literally Everything
Global Variables
BASEN_DIGITS
:List[str]
of default digits for baseN
, default digits are defined for bases 2-36 and 64BASE_DIGITS
:Dict[int, List[str]]
of all default digits
Classes
Integer
:Integer
class
Class Properties
Integer.base_digits
:Dict[int, List[str]]
of all default digits for theInteger
, updated byInteger.get_digits
Integer.dec_value
:int
of the decimal value of theInteger
, never changes after initialization
Class Methods
Integer.__init__(self, n: Union[int, str], base: int = 10, digits: List[str] = []) -> None
: Takesn
(str
preferred,int
is okay ifbase
is10
) and converts it frombase
(int
) to decimal and stores inInteger.dec_value
,digits
(List[str]
) is required ifbase
does not have default digits (seeBASEN_DIGITS
)Integer.to_base(self, base: int, digits: List[str] = []) -> str
: ConvertsInteger.dec_value
tobase
(int
), and converts it frombase
(int
) to decimal and stores inInteger.dec_value
,digits
(List[str]
) is required ifbase
does not have default digits (seeBASEN_DIGITS
)Integer.to_dec(self) -> int
: ReturnsInteger.dec_value
Integer.to_bin(self) -> str
: UsesInteger.to_base
to convertInteger.dec_value
to binaryInteger.to_octal(self) -> str
: UsesInteger.to_base
to convertInteger.dec_value
to octalInteger.to_hex(self) -> str
: UsesInteger.to_base
to convertInteger.dec_value
to hexadecimalInteger.to_base64(self) -> str
: UsesInteger.to_base
to convertInteger.dec_value
to base 64Integer.get_digits(self, base: int, digits: List[str]) -> List[str]
: Validatesdigits
(which is aList[str]
by removing duplicates and checking if the number of digits matches the base), and returns either default digits (ifbase
has defined default digits anddigits
is empty) or the validateddigits
(either ifbase
is an abnormal base orbase
is a known base but the digits are overridden), this method is called byInteger.__init__
Integer.remove_dupl_digits(self, l: List[str]) -> List[str]
: Removes duplicates froml
(List[str]
) and returns the newList[str]
, this method is called byInteger.get_digits
Things to Know
- When providing the
digits
property, make sure the digits are in order from lowest value to highest value, for example, thedigits
property for hexadecimal would look like this:list('0123456789ABCDEF')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
basencode-1.1.0.tar.gz
(4.3 kB
view hashes)
Built Distribution
Close
Hashes for basencode-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 100d9032a842bfdd46d002cb85bfc12d6bcedd4a70861734de49a4c2a06e9d9f |
|
MD5 | 8c2f07419de4633b38469903a8031899 |
|
BLAKE2b-256 | 804c56fb668e00986ff5e8289a4b26fb4cba1d8486067a301d954ca3501bdb45 |