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 *
>>> n1 = Number(12345) # Number constructs an Integer (in this case), Integer can be called too
# Default digits are used
>>> n1.repr_in_base(64)
'30V'
# Construct Integer from base 64
>>> Number('30V', 64)
Integer(12345)
>>> n1.repr_in_base(8)
'30071'
>>> n1.repr_in_octal()
'30071'
>>> n1.repr_in_bin()
'11000000111001'
# Overriding default digits
>>> n1.repr_in_base(2, list('-+'))
'++------+++--+'
>>> n1.repr_in_bin()
'++------+++--+'
>>> n1.repr_in_base(33)
'bb3'
>>> from string import digits, ascii_letters, punctuation
>>> n1.repr_in_base(76, list(digits + ascii_letters + punctuation[:14]))
'2ax'
>>> n1.repr_in_base(76) # Digits provided above were stored as default digits for base 76
'2ax'
>>> n2 = Integer(54321)
>>> n1 + n2
Integer(66666)
>>> n2 - n1
Integer(41976)
>>> n1 * n2
Integer(670592745)
# Both true and floor division return an Integer and perform the same functionality (as of now)
>>> n2 / n1
Float(4.400243013365737)
>>> n2 // n1
Integer(4)
>>> n2 % n1
Integer(4941)
>>> divmod(n2, n1)
(Integer(4), Integer(4941))
# Operations can also be performed with ints, though Integers will be returned
>>> n1 == 12345
True
>>> n1 + 54321
Integer(66666)
# Modes can be used to indicate how the representation should be
>>> n1.repr_in_base(64, mode='s') # 'mode' is 's' by default
'30V'
>>> n1.repr_in_base(2, digits=['--', '++'], mode='l')
['++', '++', '--', '--', '--', '--', '--', '--', '++', '++', '++', '--', '--', '++']
# Floats can be converted back and forth too
>>> Float(10.1, 2)
Float(2.5)
>>> Float(10.1, 2).repr_in_base(8)
'2.4'
>>> Float(22.22, 3).repr_in_base(7)
'11.613613613613613611501...'
TODO
- Use
decimal.Decimal
s inFloat
s to reduce miscalculation
Documentation
Global Variables
ALL_DIGITS
:str
of all digits used in default digitsBASE_DIGITS
:dict[int, list[str]]
of all default digits for bases 1-64RADIX_POINT
:str
- the default radix point forFloat
s
Number class
The Number
class is a sort of wrapper that creates an Integer
or a Float
depending on the number provided.
Integer class
Class Properties
base_digits
:dict[int, list[str]]
of all default digits for theInteger
, updated byInteger._get_digits
dec_value -> int
: This getter returns the decimal value of theInteger
as anint
which is stored inInteger._dec_value
Class Methods
__init__(self, n: int | str | tuple[int | str], base: int = 10, digits: list[str] = []) -> None
: Takesn
, converts it frombase
(int
) to decimal, stores inInteger._dec_value
;digits
(list[str]
) is required ifbase
does not have default digits (seeBASE_DIGITS
)repr_in_base(self, base: int, digits: list[str] = [], mode: str = 's') -> str | list[str]
: ConvertsInteger._dec_value
tobase
(int
), and returns the result as a astr
ifmode
(int
) is's'
, or alist
ifmode
is'l'
;digits
(list[str]
) is required ifbase
does not have default digits (seeBASE_DIGITS
)repr_in_dec(self, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(10, mode=mode)
(seeInteger.repr_in_base
)repr_in_bin(self, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(2, mode=mode)
(seeInteger.repr_in_base
)repr_in_octal(self, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(8, mode=mode)
(seeInteger.repr_in_base
)repr_in_hex(self, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(16, mode=mode)
(seeInteger.repr_in_base
)repr_in_base64(self, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(64, mode=mode)
(seeInteger.repr_in_base
)
Float class
Class Properties
base_digits
:dict[int, list[str]]
of all default digits for theFloat
, updated byFloat._get_digits
dec_value -> float
: This getter returns the decimal value of theFloat
as afloat
which is stored inFloat._dec_value
Class Methods
__init__(self, n: int | str | tuple[int | str], base: int = 10, digits: list[str] = [], radix_point: str = RADIX_POINT) -> None
: Takesn
, converts it frombase
(int
) to decimal, stores inFloat._dec_value
;digits
(list[str]
) is required ifbase
does not have default digits (seeBASE_DIGITS
)repr_in_base(self, base: int, digits: list[str] = [], radix_point: str = RADIX_POINT, mode: str = 's') -> str | list[str]
: ConvertsFloat._dec_value
tobase
(int
), and returns the result as a astr
ifmode
(int
) is's'
, or alist
ifmode
is'l'
;digits
(list[str]
) is required ifbase
does not have default digits (seeBASE_DIGITS
)repr_in_dec(self, radix_point: str = RADIX_POINT, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(10, radix_point=radix_point, mode=mode)
(seeFloat.repr_in_base
)repr_in_bin(self, radix_point: str = RADIX_POINT, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(2, radix_point=radix_point, mode=mode)
(seeFloat.repr_in_base
)repr_in_octal(self, radix_point: str = RADIX_POINT, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(8, radix_point=radix_point, mode=mode)
(seeFloat.repr_in_base
)repr_in_hex(self, radix_point: str = RADIX_POINT, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(16, radix_point=radix_point, mode=mode)
(seeFloat.repr_in_base
)repr_in_base64(self, radix_point: str = RADIX_POINT, mode: str = 's') -> str | list[str]
: Returnsrepr_in_base(64, radix_point=radix_point, mode=mode)
(seeFloat.repr_in_base
)
Things to Note
-
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')
-
By default, the default digits for all bases up to base 64 go as so:
- Digits 0-9
- Lowercase alphabet
- Uppercase alphabet
+
and/
therefore,
Number('DF', 16)
without overriding the digits for base 16 will throw an error because hexadecimal only has the digits 0-f (lowercase "f").Number('df', 16)
will constructInteger(223)
.
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
Built Distribution
Hashes for basencode-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2a68afb9b4c392b8383830163c07f7cf930b44c3da195d3e5bd16c0d86cc089 |
|
MD5 | 8b6a907289e246dfb63d92fd578cf5de |
|
BLAKE2b-256 | b1013cee33e74bb886259edc10945e9e2106636949aaae9a579bd54c74205ca8 |