Skip to main content

bin-dec-conversions

Python package that performs conversions between decimal and binary numbers. It can be used as a library, from the command line, or through an interactive prompt.

Features

  • Convert binary strings to decimal integers and the other way around
  • Optional zero-padding to a minimum width when producing binary output
  • Clear, descriptive error messages for invalid input
  • Command-line interface with flags, plus an interactive menu when run without arguments
  • Fully tested with pytest, including a round-trip check for every value from 0 to 255

Requirements

  • Python 3.10 or newer
  • questionary (installed automatically, used for the interactive menu)

Installation

pip install bin-dec-conversions

Usage

As a library

from bin_dec_conversions import binary_to_decimal, decimal_to_binary

binary_to_decimal("1010")  # 10
decimal_to_binary(10)  # "1010"
decimal_to_binary(10, 8)  # "00001010"  (zero-padded to 8 digits)
decimal_to_binary(5, 2)  # "101"       (width is a minimum, never truncates)
Function Description
binary_to_decimal(binary: str) -> int Converts a string of 0s and 1s to an integer.
decimal_to_binary(decimal: int, width: int = 0) -> str Converts a non-negative integer to a binary string, left-padded with zeros to at least width digits.

Both functions raise ValueError on invalid input:

  • binary_to_decimal rejects empty strings and any character other than 0 and 1 (including spaces, signs, prefixes like 0b, underscores and decimal points). The message lists every offending character.
  • decimal_to_binary rejects negative numbers and negative widths.
>>> binary_to_decimal("1a2")
ValueError: '1a2' is not a valid binary number: invalid character(s) '2', 'a'. Only 0 and 1 are allowed

From the command line

# Binary to decimal
bin-dec-conversions -d 1010
# The decimal equivalent of 1010 is: 10

# Decimal to binary
bin-dec-conversions -b 10
# The binary equivalent of 10 is 1010

# Decimal to binary, zero-padded to 8 digits
bin-dec-conversions -b 10 -w 8
# The binary equivalent of 10 is 00001010
Option Description
-d BINARY, --to-decimal BINARY Convert a binary number to decimal.
-b DECIMAL, --to-binary DECIMAL Convert a decimal number to binary.
-w WIDTH, --width WIDTH Minimum number of binary digits, zero-padded. Only valid together with --to-binary.

--to-decimal and --to-binary are mutually exclusive.

Interactive mode

Run the command with no arguments to open an interactive menu:

bin-dec-conversions

You can choose Binary to decimal, Decimal to binary (with an optional width), or Exit. Invalid input is reported and you are prompted again.

Development

Clone the repository and install the package in editable mode with the development dependencies:

git clone https://github.com/JustM4rk123/bin-dec-conversions.git
cd bin-dec-conversions
pip install -e ".[dev]"

Run the tests and the linter:

pytest
ruff check .

Project structure

src/bin_dec_conversions/
├── __init__.py   # public API and __version__
├── core.py       # conversion functions
└── cli.py        # command-line and interactive interface
tests/
├── test_conversions.py
└── test_invalid.py

License

Released under the MIT License.

Author: Marco S. Tornabene

Metadata

Release files for bin-dec-conversions 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bin-dec-conversions 1.0.0
File Size Uploaded Provenance Actions
bin_dec_conversions-1.0.0.tar.gz 5.7 kB Without provenance Details for bin_dec_conversions-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bin-dec-conversions 1.0.0
File Interpreter ABI Platform Actions
bin_dec_conversions-1.0.0-py3-none-any.whl Python 3 none any Details for bin_dec_conversions-1.0.0-py3-none-any.whl

Total release size: 11.6 kB

Release files / bin_dec_conversions-1.0.0.tar.gz

Download URL bin_dec_conversions-1.0.0.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b350472a4795575b54638a2f3bcdd130bca0e252c6c99d7e8f43a1ca2e15f350
BLAKE2b-256 checksum
How to use checksums
7e317c05f3e69ec44787624663ff177e384b6765816dd710f5a96de03642fd4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / bin_dec_conversions-1.0.0-py3-none-any.whl

Download URL bin_dec_conversions-1.0.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
571b1bef3ef3a150ab1cabc64f2a1e7b62d72a82c9d9889932ac334cc0cd5616
BLAKE2b-256 checksum
How to use checksums
adb5bacbd7ca2d244093f9a35dd7818e183a5df209f176f8068013e78257a4c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for bin-dec-conversions 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page