Base2048
`base2048` is a Python library that provides efficient binary encoding and decoding using the Base2048 scheme, implemented as a Rust extension for performance. It offers simple functions to convert bytes to Base2048 strings and vice-versa. The current version is 0.1.3, and it functions as a stable utility library with infrequent updates, focusing on its core encoding capabilities.
Common errors
-
TypeError: argument 'data': 'str' object cannot be converted to 'bytes'
cause Attempting to encode a Python `str` object directly with `base2048.encode()`.fixConvert the string to bytes first: `encoded_data = base2048.encode('my string'.encode('utf-8'))`. -
TypeError: argument 'data': 'bytes' object cannot be converted to 'str'
cause Attempting to decode a Python `bytes` object directly with `base2048.decode()`.fixThe input to `base2048.decode()` must be a Python `str`. Ensure you are passing the string output from `base2048.encode()` or a valid Base2048 string. -
ModuleNotFoundError: No module named 'base2048'
cause The `base2048` library has not been installed in the current Python environment.fixInstall the library using pip: `pip install base2048`.
Warnings
- gotcha The `base2048.encode()` function strictly expects `bytes` objects as input. Passing a Python `str` directly will result in a `TypeError` because the library operates on raw binary data.
- gotcha The `base2048.decode()` function strictly expects a Python `str` object (the Base2048 encoded string) as input. Passing a `bytes` object will result in a `TypeError`.
- gotcha Base2048 is a binary encoding scheme and does not intrinsically handle character encodings (like UTF-8, Latin-1, etc.). If you are encoding text, you must explicitly encode your `str` to `bytes` before passing it to `encode()` and then decode the resulting `bytes` back to a `str` after calling `decode()`.
Install
-
pip install base2048
Imports
- encode
from base2048 import encode
- decode
from base2048 import decode
Quickstart
from base2048 import encode, decode
original_bytes = b"Hello, Base2048 encoding!"
# Encode bytes to a Base2048 string
encoded_string = encode(original_bytes)
print(f"Original bytes: {original_bytes!r}")
print(f"Encoded string: {encoded_string!r}")
# Decode Base2048 string back to bytes
decoded_bytes = decode(encoded_string)
print(f"Decoded bytes: {decoded_bytes!r}")
assert original_bytes == decoded_bytes
# Example with text that needs explicit encoding/decoding
text = "こんにちは, Base2048!"
text_bytes = text.encode('utf-8')
encoded_text_string = encode(text_bytes)
print(f"\nOriginal text: {text!r}")
print(f"Encoded text string: {encoded_text_string!r}")
decoded_text_bytes = decode(encoded_text_string)
decoded_text = decoded_text_bytes.decode('utf-8')
print(f"Decoded text: {decoded_text!r}")
assert text == decoded_text