BaseConverter
BaseConverter is a calculator written to convert numbers betwewen arbitrary bases. It takes a direct approach of converting the number without first shifting to decimal base.
WHAT IT IS
Since Python's int
function has the shortcoming of only converting to base 10
, this program was developed to let you convert to arbitrary bases, that int
can convert from, i.e. 2
to 36
.
This program also gives you the additional feature, to let you convert custom defined bases, to a system defined, or another custom defined one.
Hence the conversion of (102421)
5
to base 32
is successful as shown in the provided screenshot.
And also the conversion of *BAD
with the from and to digit spaces, respecetively defined as
f = {
'digit': 'ABCDE',
'sign': '*'
} # Base is derived as len(digit)
t = {
'digit': 'CD',
'sign': '+'
} # Base is derived as len(digit)
is successful. And it transforms to +DDDCC
.
TODO
- Implement a system to let user enter their own system of digit, and accordingly convert inspired by python-baseconv
- Include working examples in a separate file, which imports the package, and tests it
- Implement floating point conversion
- Let user specify precision of conversion wanted
- Write a PyPI package for this repo, so users can utilise it more easily, by doing a
pip install
- Implement
n
's and(n-1)
's complement method of calculation
INFO
- User can now convert using their own digit space, of any base. (Base is defined as length of your digit space. Hence
'abcd1234'
, is Base8
) - Base is case-sensitive, hence
A
anda
are not same - Repeated characters in base, or sign in digit-space, will lead to error, just as it should.
- Program will calculate the base on its own for your given custom base input. Hence you need only to enter any of the digit-space, sign, and/or separator
- There is some loss in accuracy in conversion of floating points, as would be expected, hence the output if feeded back, may not give the exact input back
FUNCTIONS AND MODULE VARIABLES
- A few variables as defined in the module are:
sys
- A dictionary containing the system defined values as follows:'digit'
- The system maximum base digit space'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'sign'
- The system negative symbol-
'sep'
- The system defined separator.
'prec'
- The default precision6
f
- A dictionary containing the data for the from baset
- A dictionary containing the data for the to basef
andt
have the same keyword values assys
, with the expection of'prec'
, instead there is'base'
, which is the length of'digit'
- To facilitate easy definition and modification of bases, a few functions have been provided:
setPrec(pre = 6)
- Used to set the precision, resetting to6
on no argsmodifyF(fBase = 36)
- With no arguments resetsf
to the system base36
modifyT(tBase = 36)
- With no arguments resetst
to the system base36
modifyDef(fBase, tBase)
- calls bothmodifyF
andmodifyT
customF(digit = sys['digit'], sign = sys['sign'], sep = sys['sep'])
- Sets the respective values inf
with the data passed or with system defaults, insys
customT(digit = sys['digit'], sign = sys['sign'], sep = sys['sep'])
- Sets the respective values int
with the data passed or with system defaults, insys
customDef(fDigit = sys['digit'], fSign = sys['sign'], fSep = sys['sep'], tDigit = sys['digit'], tSign = sys['sign'], tSep = sys['sep'])
- Calls bothcustomF
andcustomT
with the data passedconverter(number, digit, sign)
- Uses the algorithm explained inexplanation.md
to convert the number in integer formfraction(number, digit, sign, pre)
- Uses the algorithm explained inexplanation.md
to convert the number in its fractional formtransform(number)
- Transforms number from that defined inf
to that int
validate()
- Validates if the digit space, and corresponding data is validnumberCheck(number)
- Validates if input number is valid in definedf
digit spaceconvert(number, fro = f, to = t, pre = 0)
- The function which makes all the magic happen normally speakingfro
is a dictionary used for single conversion custom Baseto
is a dictionary used for single conversion custom Basepre
is an int to set precision for only one conversion
NOTES
- If when defining a custom Base, using
customF
orcustomT
, instead of wanting to pass, individual arguments, you can pass a dictionary containing any of the following keywords, in the given mannercustomF(**<dict-name>)
, orcustomT(**<dict-name>)
:'digit'
- The digit space'sign'
- The sign to be used'sep'
- The separator to be used
- If it is being done via
customDef
, then it is to be passed in the same manner as above, and the keywords to be present in the dictionary are:'fDigit'
- The digit space forf
'fSign'
- The sign to be used forf
'fSep'
- The separator to be used forf
'tDigit'
- The digit space fort
'tSign'
- The sign to be used fort
'tSep'
- The separator to be used fort
EXAMPLES
To see a few working tests you can check the test.py
file.
EXPLANATION
To understand how the algorithm works, please check explanation.md
.
Install and Usage
The repo can be directly cloned, and you can import the module in current directory.
Example usage:
>>> # To use user defined bases
>>> import converter
>>> fro = {
... 'digit': '24',
... 'sign': '*',
... }
>>> converter.convert('*244242', t = {'digit': 24681379}, f = fro)
'+86'
>>>
>>> # To use a system defined fro, and user defined to
>>> converter.modifyF(5) # Modifies your fro base to 5, and digit space to '01234'
>>> converter.customT(2468)
>>> converter.convert(21)
'68'
>>>
>>> # For user defined fro, and system to, just pass fro as f = <dict-name>
>>> # And first modify to using converter.modifyT(<base>)
>>> # For both system defined, we can call the modify functions seperately or
>>> # modifyDef(<froBase>, <toBase>) can be called
>>> converter.modifyDef(5, 32)
>>> converter.convert('00141')
'1E'
>>>
>>> converter.customF('😁😃', sep = '➗')
>>> converter.modifyT(4)
>>> converter.convert('-😁😃😃😁')
'-12'
>>> converter.convert('-😁😃➗😁😃😃')
'-1.12'
>>> converter.convert('😁😃➗😃😁😃')
'1.22'
>>>
>>> converter.modifyF(4)
>>> converter.customT('😁😃', sep = '➗')
>>> converter.convert(-1.3)
'-😃➗😃😃'
>>> converter.convert(-1.12)
'-😃➗😁😃😃'
>>> converter.setPrec(4)
>>> converter.convert(.123)
'-😁➗😁😃😃😁'
>>> converter.convert(.123, pre = 10)
'-😁➗😁😃😃😁😃😃'
To get a list of functions in the module run dir(converter)
, or help(converter)
.
To get help on respective functions run help(converter.<function-name>)
.