cobs
raw JSON → 1.2.2 verified Mon Apr 27 auth: no python
Consistent Overhead Byte Stuffing (COBS) implementation for Python. Encodes arbitrary binary data into a form that removes any occurrence of a specific delimiter byte (typically 0x00), with minimal overhead. Current version 1.2.2 supports Python >=3.6. Development is stable with infrequent releases.
pip install cobs Common errors
error AttributeError: module 'cobs' has no attribute 'encode' ↓
cause Imported cobs package instead of cobs.cobs submodule.
fix
Change to 'import cobs.cobs as cobs' or 'from cobs import cobs'
error ValueError: decoded data contains unexpected zero byte ↓
cause Encoded data includes a delimiter byte (0x00) that is not the final byte, or the decoder was given raw data.
fix
Ensure you pass properly COBS-encoded data. Use cobs.encode() to encode before transmission.
error cobs.decode() returns empty bytes when input contains only a trailing zero ↓
cause A single 0x00 byte decodes to an empty byte string (valid COBS). This might be unexpected.
fix
Check for empty decoded output if protocol expects non-empty data.
Warnings
deprecated COBSR (reducing overhead variant) module cobs.cobsr is less tested and may have edge cases. ↓
fix Use cobs.cobs for compatibility unless you specifically need reduced overhead.
gotcha The encode() function does NOT add a trailing delimiter byte. You must add it yourself if your protocol expects it. ↓
fix encoded = cobs.encode(data) + b'\x00'
gotcha The decode() function expects the encoded data to NOT contain the delimiter byte (except possibly the final byte). Incorrectly encoded data may raise ValueError. ↓
fix Ensure your encoded data does not include delimiter bytes (except the optional trailing delimiter).
Imports
- cobs wrong
import cobscorrectimport cobs.cobs as cobs - cobsr wrong
from cobs import cobsrcorrectimport cobs.cobsr as cobsr
Quickstart
import cobs.cobs as cobs
data = b'\x00\x01\x02\x00\x03'
encoded = cobs.encode(data)
print('Encoded:', list(encoded))
decoded = cobs.decode(encoded)
print('Decoded:', list(decoded))