Skip to main content

Binary encoding with Base2048 in Rust.

Project description

Base 2048   pypi_badge versions

build_badge Rust Tests Python Tests

codecov pre-commit.ci status

When Base 64 is not enough

Allows up to 11 bits of data per unicode character as counted by social media and chat platforms such as Twitter and Discord.

Uses a limited charset within the Basic Multilingual Plane.

Based on, and uses a compatible encoding table with the Rust crate rust-base2048.

- Charset displayable on most locales and platforms

- No control sequences, punctuation, quotes, or RTL characters

Getting Started

pip install base2048
import base2048

base2048.encode(b'Hello!')
# => 'ϓțƘ໐µ'

base2048.decode('ϓțƘ໐µ')
# => b'Hello!'

Up to 2x less counted characters compared to Base 64

import zlib
import base64

import base2048

string = ('🐍 🦀' * 1000 + '🐕' * 1000).encode()
data = zlib.compress(string)

b64_data = base64.b64encode(data)
# => b'eJztxrEJACAQBLBVHNUFBBvr75zvRvgxBEkRSGqvkbozIiIiIiIiIiIiIiIiIiIiIiJf5wAAAABvNbM+EOk='
len(b64_data)
# => 84

b2048_data = base2048.encode(data)
# => 'ը྿Ԧҩ২ŀΏਬйཬΙāಽႩԷ࿋ႬॴŒǔ०яχσǑňॷβǑňॷβǑňॷβǯၰØØÀձӿօĴ༎'
len(b2048_data)
# => 46

unpacked = zlib.decompress(base2048.decode(b2048_data)).decode()
len(unpacked)
# => 4000
unpacked[2000:2002]
# => '🦀🐍'

Decode errors are provided with a character-position of failure

----> base2048.decode('༗ǥԢΝĒϧǰ༎ǥ')

DecodeError: Unexpected character 8: ['ǥ'] after termination sequence 7: ['༎']
  • To catch the error, use either base2048.DecodeError or its base exception, ValueError.
import base2048

try:
    base2048.decode('🤔')
except base2048.DecodeError as e:
    print(e)

License

The code in this project is released under the MIT License.

Related and prior works

Javascript - base2048

Rust - rust-base2048

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

base2048-0.1.3.tar.gz (91.4 kB view hashes)

Uploaded Source

Built Distributions

base2048-0.1.3-cp37-abi3-win_amd64.whl (122.9 kB view hashes)

Uploaded CPython 3.7+ Windows x86-64

base2048-0.1.3-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view hashes)

Uploaded CPython 3.7+ manylinux: glibc 2.17+ x86-64

base2048-0.1.3-cp37-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (448.2 kB view hashes)

Uploaded CPython 3.7+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page