📄 cobs_encode.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 + -