Convert numbers between different bases.
Project description
This package allows you to convert a number between any of the 82 standard bases described by Wikipedia and here. It provides both commandline options and in code options to convert.
In Command Line
There are several ways to use from command line with python -m convert_numbers.* command.
a) convert_numbers.convert
python -m convert_numbers.convert number new_base --current_base python -m convert_numbers.convert number new_base -cb python -m convert_numbers.convert 12 2 -cb 10
b) convert_numbers.loop
This converts the input and waits for more input to convert until it’s quit. The user types in 3 numbers separated by a space.
number_to_convert base_to_convert_to current_base
If current_base is not provided base 10 is assumed. Runs until user types ‘q’. For help type ‘h’.
python -m convert_numbers.loop Enter 'number new_base current_base': number new_base current_base* Enter 'number new_base current_base': 12 2 10* '12' in Decimal (base 10) to Binary (base 2) = '1100' Enter 'number new_base current_base': q
c) convert_numbers.table
python -m convert_numbers.table 12,13,14 2 -cb=16 Base 16 to base 2 ---------------------------- | Hexadecimal | Binary | ---------------------------- | 12 | 10010 | | 13 | 10011 | | 14 | 10100 | ----------------------------
An example of input file is found here.
d) convert_numbers.file
This takes input completely from a file and outputs to another file. In the file, the first line is the list of comma-separated numbers to convert, the second is the new base, while the third is the current base, if any otherwise it’s base 10. Example input is here
python -m convert_numbers.file input_file.txt output_file.txt Written to 'out_file.txt'
Writes a csv like file, two columns old number, new number. The first two rows are informational.
In python code
Several functions are described by the package. They are convert_number, convert_from_base_10, convert_float_from_base_10, convert_to_base_10 and convert_float_to_base_10. To check user input refer to this section
Main Functions
These, only one, can be used in isolation to convert a number from any base to any other base.
1. convert_number
Usage
from conversions import convert_number
convert_number('12', 10, 2) # return '1100'
Helper Functions
1. convert_from_base_10
Usage
from conversions.convert import convert_from_base_10
result = convert_from_base_10(12, 2) # returns ['1100', 2]
2. convert_float_from_base_10
Usage
from conversions.convert import convert_float_from_base_10
result = convert_float_from_base_10(0.25, 2) # ['0.01', 2]
3. convert_to_base_10
from conversions.convert import convert_to_base_10
result = convert_to_base_10('1001', 2) # returns 9
4. convert_float_to_base_10
from conversions.convert import convert_float_to_base_10
result = convert_float_to_base_10('0.2', 16) # returns 0.125
Utility functions
To check the name of a base given the base, use n_utils.base_find which takes the base as argument and returns a string of the name of the base.
from n_utils import base_find, ensure_base_is_standard, base_limits
ensure_base_is_standard(63) # False
ensure_base_is_standard(64) # True
base_limits('G4', 16) # False
base_limits('G4', 20) # True
base_find(2) # Binary
base_find(10) # Decimal
Project details
Release history Release notifications | RSS feed
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 number_conversions-0.0.7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f4bdf3293595562b39e051a62dc9a1928cae92b683af5774c0706bece2cc8683 |
|
MD5 | 8bae2c0d4f50b82ce56d7f3ed54e0166 |
|
BLAKE2b-256 | c1b6474c52810b14ff376fc7a9c802dfc01ef06c1185994da7df330c6b21d716 |