Consistent Overhead Byte Stuffing (COBS) is a clever way to prepare data for transmission so that a specific byte (usually 0x00) never appears in the message body, allowing it to be used reliably as a "end of packet" marker. Unlike traditional "escape-character" stuffing, which can double the size of your data in the worst case, COBS has a guaranteed maximum overhead of only 1 byte for every 254 bytes of data.
COBS breaks your data into "code blocks." Each block starts with a code byte that tells the receiver how many bytes to skip to reach the next code byte.
- Append a "Phantom" Zero: Conceptually, treat the end of your data as if there is a zero byte there.
- Scan for Zeroes: Look at the first 254 bytes of your data.