⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cobs_encode.cpp

📁 COBS-consistent overhead byte stuffing
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -