Skip to main content

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

Project description

philiprehberger-base-convert

Tests PyPI version GitHub release Last updated License Bug Reports Feature Requests Sponsor

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"

Bytes Encoding

Encode arbitrary bytes (UUIDs, hashes, binary data) to compact base-N strings:

from philiprehberger_base_convert import base62

# Encode bytes to base62
data = b"\x00\x01\x02\xff"
encoded = base62.encode_bytes(data)

# Decode back to bytes
decoded = base62.decode_bytes(encoded)
assert decoded == data

# Encode a UUID to a compact string
import uuid
uid = uuid.uuid4().bytes
short_id = base62.encode_bytes(uid)

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

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
BaseCodec.encode_bytes(data) Encode bytes to a base-N string (preserves leading zero bytes)
BaseCodec.decode_bytes(value) Decode a base-N string back to bytes
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

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.

LinkedIn More packages

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.2.0.tar.gz (6.3 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.2.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8a3a117895261e9f25a56feb34eecec0be42accce051ffb224ee2df09b9e2e98
MD5 78fc5bbc4e0d040d21cf02964620423e
BLAKE2b-256 e19a8d7df00ae1afd2b81068237c8f093996ea1eb2651deef75409c95c0575f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af7bcaeea1c90cce1d8aae37df3de749b274711bf632d839002ad1bf2fd7151e
MD5 d3677c23ba97e0c85c421d53d9b7b217
BLAKE2b-256 09244b1c5a2c942a6f7746f909f12dd29e522545187cdecc920672abb5d25b1a

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