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
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.1.3.tar.gz.
File metadata
- Download URL: philiprehberger_base_convert-0.1.3.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ede959a4cb3f3479f0497f5489a7451a5143202521999bd18500e18f2bdeef81
|
|
| MD5 |
7f2a3b890f838016bcb9166a3f03c579
|
|
| BLAKE2b-256 |
278f78bad46f41c0476b62aab8a69ee69f4e532b36cc577ea9a206a712ca6ade
|
File details
Details for the file philiprehberger_base_convert-0.1.3-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_base_convert-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.6 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 |
9b3ed86cb2aa1e725fcf8cebad934e19b203e98432efa138f2ff25e6609b73e3
|
|
| MD5 |
bef127e7088ea21f0ca0b5f417e518c3
|
|
| BLAKE2b-256 |
e045c93b40eb11adb3d3494b03a98297466e2e2c525f51a4ee9999e0bb00bd4a
|