cobs_encode.cpp

来自「COBS-consistent overhead byte stuffing」· C++ 代码 · 共 46 行

CPP
46
字号
// COBS_encode.cpp : Defines the entry point for the console application.
// Generally, sizeof[result] = sizeof [source] +2; First byte is the length, last byte is 00 (delimiter).

#include "stdafx.h"

int main(int argc, char* argv[])
{
	
	
	unsigned char source[] = {0,0,0,0,22,0,0,55,0,0,0};			//Max length = 11
	unsigned char result[13];						// Max length = 13, after COBS encode, length is added by 2 always.
	unsigned char *pSource = source;
	unsigned char *pDestination = result;
	unsigned char length = sizeof(source);			//length of source
	
	unsigned char *pEnd = source + length;
	unsigned char *codePtr = pDestination++;
	unsigned char code = 0x01;

	
	while ( pSource < pEnd)
	{
		if (*pSource == 0)
		{
			*codePtr= code;
			codePtr = pDestination++;
			code = 0x01;
		}
		else
		{
			*pDestination = *pSource;

			++pDestination;
			code++;
		}

		++pSource;
	}
	
	*codePtr = code;
	*pDestination = 0x00;

	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?