COBS Encoder/Decoder

Consistent Overhead Byte Stuffing (COBS) removes one chosen byte value, usually 00, from the data, so that this byte can mark the end of every packet in a stream. Enter bytes in HEX, choose the delimiter and press Encode; to decode, paste a whole encoded packet with the delimiter at the end.

Source data

Processed data

What COBS is for

When packets go over a serial line (UART, RS-485), a USB virtual COM port or any other byte stream, the receiver has to find where one packet ends and the next one starts. The simplest way is to reserve one byte value as a delimiter, but the data itself may contain that byte. COBS encodes the packet so that the delimiter never appears inside it, and the receiver just splits the stream at every delimiter.

The overhead is small and fixed: one code byte per up to 254 data bytes, plus the delimiter at the end. Escape-based byte stuffing (SLIP, HDLC) adds a byte for every special byte in the data and can double the size in the worst case. If a byte is lost, the receiver loses only the current packet and resynchronizes at the next delimiter.

How it works

Every delimiter in the data is replaced with a code byte: the distance to the next delimiter. One more code byte goes at the start, and the encoded packet ends with the delimiter. For the delimiter 00 the data 00 11 11 00 22 22 00 is encoded as 01 03 11 11 03 22 22 01 00: the first 01 says that the next 00 comes right after it, 03 means two data bytes and then 00, the last 01 marks the end of the data. The decoder follows the code bytes and puts the delimiter back at every position they point to.

A code byte can count at most 254 data bytes. The code FF means 254 data bytes with no delimiter after them, and the next code byte follows. For a delimiter other than 00 every code byte is XORed with the delimiter, so it never equals the delimiter; data bytes are copied as is. This is the same as XORing the data with the delimiter, applying standard COBS and XORing the result back.

Cookies policies