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"
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 project useful:
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_base_convert-0.2.1.tar.gz.
File metadata
- Download URL: philiprehberger_base_convert-0.2.1.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0605af530953be5db9143da465514b93a669f1d91372038d00266c8bfbe5e23
|
|
| MD5 |
e4bc28d69d897ff7500b4f4213e93f25
|
|
| BLAKE2b-256 |
da8428dd9af143144fd17a97b3f733a32d56f46289fe1bb7de5d093d35e06f93
|
File details
Details for the file philiprehberger_base_convert-0.2.1-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_base_convert-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc664ed3582db95c64006fda3adf428000340e5ad0399445f26267bd9afe0b25
|
|
| MD5 |
97be89ec349b924e085cd09e89ba50ed
|
|
| BLAKE2b-256 |
313fc0c29db593a798967336b21b32966949ab502246210b8ec2055e421d7bf5
|