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.4.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.4-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.1.4.tar.gz
Algorithm Hash digest
SHA256 cdcf4d41240a2034e52d9632834601b3ad64b8a32aa6a8cdd19b6ef927e6e880
MD5 d43b96c64b984221732ca0a0eb979885
BLAKE2b-256 0d19670b48c8ffbe395edeac01cca6fb755d611686e51afd6dc516618983dec3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_base_convert-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 10c77c15aa222a4134eaab4d7746284193c72ff220b87ee356b81a03eb3902a4
MD5 98965fb03aa27d29fd11157ed19b498a
BLAKE2b-256 e63e95708480b3bc843b6350779ee3275e89ebd321b32d7a2d00af7559321099

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