Skip to main content

Convert numbers between any base (2-62) with human-friendly APIs

Project description

philiprehberger-base-convert

Tests PyPI version License

Convert numbers between any base (2-62) with human-friendly APIs.

Installation

pip install philiprehberger-base-convert

Usage

Basic Conversion

from philiprehberger_base_convert import to_base, from_base

# Convert to base 16 (hex)
to_base(255, 16)      # "ff"
from_base("ff", 16)   # 255

# Convert to base 62
to_base(999999, 62)   # "4c91"
from_base("4c91", 62) # 999999

Pre-built Codecs

from philiprehberger_base_convert import base16, base36, base58, base62

base62.encode(123456789)       # "8M0kX"
base62.decode("8M0kX")         # 123456789

base58.encode(123456789)       # Bitcoin-style base58
base36.encode(123456789)       # "21i3v9"
base16.encode(255)             # "ff"

Custom Alphabet

from philiprehberger_base_convert import to_base, from_base, BaseCodec

# Use a custom alphabet for base 3
to_base(42, 3, alphabet="XYZ")   # "YXZX"
from_base("YXZX", 3, alphabet="XYZ")  # 42

# Reusable codec with custom alphabet
codec = BaseCodec(4, alphabet="ACGT")
codec.encode(42)   # "GCAC"
codec.decode("GCAC")  # 42

API Reference

Function / Class Description
to_base(number, base, *, alphabet="") Convert int to string in given base (2-62)
from_base(value, base, *, alphabet="") Convert string in given base back to int
BaseCodec(base, *, alphabet="") Reusable encoder/decoder for a fixed base
BaseCodec.encode(number) Encode int to string
BaseCodec.decode(value) Decode string to int
base16 Pre-built codec for base 16
base32 Pre-built codec for base 32
base36 Pre-built codec for base 36
base58 Pre-built codec for base 58 (Bitcoin alphabet)
base62 Pre-built codec for base 62

License

MIT

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

philiprehberger_base_convert-0.1.3.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_base_convert-0.1.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_base_convert-0.1.3.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.1.3.tar.gz
Algorithm Hash digest
SHA256 ede959a4cb3f3479f0497f5489a7451a5143202521999bd18500e18f2bdeef81
MD5 7f2a3b890f838016bcb9166a3f03c579
BLAKE2b-256 278f78bad46f41c0476b62aab8a69ee69f4e532b36cc577ea9a206a712ca6ade

See more details on using hashes here.

File details

Details for the file philiprehberger_base_convert-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9b3ed86cb2aa1e725fcf8cebad934e19b203e98432efa138f2ff25e6609b73e3
MD5 bef127e7088ea21f0ca0b5f417e518c3
BLAKE2b-256 e045c93b40eb11adb3d3494b03a98297466e2e2c525f51a4ee9999e0bb00bd4a

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