Skip to main content

Allows the conversion of a value with any base to the corresponding value in any other base-system

Project description

Arbitrary Base to Arbitrary Base Converter

Allows the conversion of a value in a given base(a) to the corresponding value in some other base(b). The symbols used in both the base-systems can be provided by the user.

Usage:

change_base.change_base(value, initial_base, final_base, initial_base_symbols, final_base_symbols)

Description:

A value from a given set with base = a is converted to the equivalent value in some other base = b. For example, decimal values (Base10) can be transformed into Hex (Base16) or Octal (Base8) values.

Transformation is achieved by first converting to decimal equivalent and then to the final base version through the following 2 functions:

  1. base_to_decimal(val, base = 73, symbols = None)
  2. decimal_to_base(d, base, symbols = None)

In the base_to_decimal function, val is the original value (inputted as a string) with base = base. symbols is a list to pass custom symbols for the system.

In the decimal_to_base function, d represents the original decimal value and symbols list stores the symbols used for this system with base = base

The symbols used to represent the system can be provided as custom input e.g. a value $$&$## may belong to a system with base = 3 and symbols #, &, $. This value can be converted to a decimal equivalent by calling:

base_to_decimal("$$&$##", 3, ['#', '&', '$'])

In case the symbol list is not provided, the default ordered symbol list is used up to maximum of (Base73). The 73 symbols used are (in this order): Digits (0-9), Lowercase-Letters (a-z), Uppercase-letters (A-Z), Safe-symbols [ $, -, _, ., +, !, *, ', (, ), , ]

Thus, each of these symbols have a decimal equivalent = list index from 0 to 73 that is used for base conversion.

The two functions are combined in the definition of function base_change. Thus, to change some value 'val' with base 'ibase' and symbol set (in order) 'isymbols' (optional argument) into the equivalent value in some base 'fbase' and symbol set 'fsymbols' (optional argument), call the function:

  • base_change(val, ibase, fbase, isymbols, fsymbols)

Example Usages:

  • Converting from Decimal to Binary: base_change('123',10,2) -> '1111011' #Output
  • Converting from Binary to Decimal: base_change('1111011',2,10) -> '123' #Output
  • Converting from Decimal to Hex: base_change('123',10,16) -> '7b'
  • Converting from Base5 (with default symbols [0,1,2,3,4]) to Base5 (custom symbols ['t','w','i','s','t']): base_change('123',5,5,None,['t','w','i','s','t']) -> 'wis'
  • Converting from Base5 (with custom symbols ['t','w','i','s','t']) to Base10 (default symbols): base_change('witsittitsisits',5,10,['t','w','i','s','t'],None) -> '9697104823'
  • Inverse of the last example: base_change('9697104823',10,5,None,['t','w','i','s','t']) -> 'witsittitsisits'

Note:

  • The position of symbol in custom symbol list decides its decimal equivalent
  • Default symbol list consists of only 73 symbols. Thus maximum base conversion with default symbols is upto Base73
  • Since the apostrophe ' sign is also usable as a default symbol, the input value strings should be enclosed with double quotes
  • All outputs are generated as strings

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

change_base-0.0.1.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

change_base-0.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file change_base-0.0.1.tar.gz.

File metadata

  • Download URL: change_base-0.0.1.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for change_base-0.0.1.tar.gz
Algorithm Hash digest
SHA256 83de4613ccd7c3a9220b5678890e51a88b6b493aaf2b699a8eb9c6d1b098a878
MD5 f28fd60dfe9b4114d2e21b4d889b0c2c
BLAKE2b-256 7be1aea9c339ffb4d08ec1d3fb8944c1d9ee3190eaf8885bc468409bba42e4aa

See more details on using hashes here.

File details

Details for the file change_base-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: change_base-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.4

File hashes

Hashes for change_base-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79b9cfc02ea2bfa56ddf31e4ec3104a3d7470da9aaae4dbf0bd8874465b6b82a
MD5 34f8245fcfc8597068eb22eb92555461
BLAKE2b-256 c6cb22809d4649221238bd0e8e761b8293689304c267da1d99f2dddb745c78e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page