cobs

package module
v1.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2025 License: ISC Imports: 0 Imported by: 6

README

Consistent Overhead Byte Stuffing

COBS encoder breaks a packet into one or more sequences of non-zero bytes. The encoding routine searches through the first 254 bytes of the packet looking for the first occurrence of a zero byte. If no zero is found, then a code of 0xFF is output, followed by the 254 non-zero bytes. If a zero is found, then the number of bytes examined, n, is output as the code byte, followed by the actual values of the (n-1) non-zero bytes up to (but not including) the zero byte. This process is repeated until all the bytes of the packet have been encoded.

Documentation

Overview

Package cobs implements Consistent Overhead Byte Stuffing algorithm

COBS encoder breaks a packet into one or more sequences of non-zero bytes. The encoding routine searches through the first 254 bytes of the packet looking for the first occurrence of a zero byte. If no zero is found, then a code of 0xFF is output, followed by the 254 non-zero bytes. If a zero is found, then the number of bytes examined, n, is output as the code byte, followed by the actual values of the (n-1) non-zero bytes up to (but not including) the zero byte. This process is repeated until all the bytes of the packet have been encoded.

See also: http://www.stuartcheshire.org/papers/COBSforToN.pdf

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(p []byte) []byte

Decode a cobs frame to a null-terminated slice of bytes

func Encode

func Encode(p []byte) []byte

Encode a null-terminated slice of bytes to a cobs frame

Example
data := "Hello"
encoded := Encode(Terminate([]byte(data)))
fmt.Printf("%v: %v\n", data, encoded)
decoded := TrimNull(Decode(encoded))
fmt.Printf("%v: %v\n", encoded, string(decoded))
Output:

Hello: [6 72 101 108 108 111]
[6 72 101 108 108 111]: Hello

func EncodedSize

func EncodedSize(n int) int

EncodedSize calculates size of encoded message

func Terminate added in v1.0.3

func Terminate(p []byte) []byte

Terminate a byte slice with null

func TrimNull added in v1.0.3

func TrimNull(p []byte) []byte

TrimNull removes null-termination

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL