Skip to content

Instantly share code, notes, and snippets.

@midnight-wonderer
Created April 5, 2026 14:24
Show Gist options
  • Select an option

  • Save midnight-wonderer/82eca87f7e4d2518d9166a65e17edd52 to your computer and use it in GitHub Desktop.

Select an option

Save midnight-wonderer/82eca87f7e4d2518d9166a65e17edd52 to your computer and use it in GitHub Desktop.
COBS Encoding

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.

How COBS Encoding Works

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.

Step-by-Step Process

  1. Append a "Phantom" Zero: Conceptually, treat the end of your data as if there is a zero byte there.
  2. Scan for Zeroes: Look at the first 254 bytes of your data.
  • If you find a zero: The code byte is the distance (index) to that zero. For example, if the first byte is 0x00, the code byte is 1.
    • If you don't find a zero: The code byte is 0xFF (255), indicating that the next 254 bytes are all non-zero data.
  1. Replace and Repeat: The found zero is removed from the data stream. The code byte is placed at the start of the block, and the process repeats from the byte immediately after the zero you just removed.
  2. Add Final Frame Marker: Once finished, you add a 0x00 at the very end to mark the boundary for the receiver.

Examples## Example 1: Basic Zero Removal

Input: 11 22 00 33

  1. Block 1: First zero is at index 3. Code byte = 03. Data = 11 22.
  2. Block 2: Remaining data is 33. We hit the "phantom" zero 2 bytes away. Code byte = 02. Data = 33.
  3. Result: 03 11 22 02 33 (Final packet followed by 00 delimiter).

Example 2: No Zeroes (Worst Case)

Input: 11 22 33 44

  1. Block 1: No zeroes found before the end. The "phantom" zero is 5 bytes away. Code byte = 05. Data = 11 22 33 44.
  2. Result: 05 11 22 33 44.

Example 3: Multiple Zeroes

Input: 00 00 00

  1. Block 1: Zero at index 1. Code = 01.
  2. Block 2: Next zero at index 1. Code = 01.
  3. Block 3: Next zero at index 1. Code = 01.
  4. Block 4: "Phantom" zero at index 1. Code = 01.
  5. Result: 01 01 01 01.

COBS Encoding Table

Unencoded Data Encoded COBS Explanation
00 01 01 First 01 points to the 00 (replaced); second 01 is the final pointer.
11 22 00 33 03 11 22 02 33 03 says "the zero was at position 3".
11 00 00 00 02 11 01 01 01 Each 01 replaces a 00 in the original data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment