Skip to main content

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

Project description

philiprehberger-base-convert

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.2.tar.gz (3.9 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.2-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7ceb3707c1b2110843b865649bda9a994d554fc9838bcd7ec131180c3a6cacb2
MD5 5eb75ecbe692c3dcd6a14b4709e5c7e9
BLAKE2b-256 458d19ebc5eb953760d50ab8baba614d0051297c20924d82157a4ddaadd08413

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ca74588155ef37895433bbb5a8907b682d7d16fb876d2162b8277fba80160380
MD5 463f3a2fcb02f967959b6ed8db9aadec
BLAKE2b-256 dffde9a14e0adf371cfd5d59ab37935b4ab639fa5b493f79f5ad94c97275b1b7

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