static auto BinaryDataSplit = [](char* buff, size_t buffsize, const int PARTSIZE, char* header = nullptr, size_t headersize=0)
{
std::vector<std::vector<char>> data;
int part = (buffsize + PARTSIZE - 1) / PARTSIZE;
for (int i = 0; i < part; i++)
{
std::vector<char> partdata;
int length = (buffsize < (i + 1) * PARTSIZE) ? buffsize - i * PARTSIZE : PARTSIZE;
char* bf = buff + (i * PARTSIZE);
partdata.resize(length + headersize);
if (header && headersize > 0)
{
std::copy(header, header + headersize, partdata.begin());
}
else headersize = 0;
std::copy(bf, bf + length, partdata.begin() + headersize);
data.push_back(std::move(partdata));
}
return data;
};
int main()
{
char data[] = "hello world";
char header[] = "head";
//将data数据分成3字节一个包,并且附带header头信息
auto udpDataParted = BInaryDataSplit(data, strnlen(data), 3, header, strlen(header));
return 0;
}
04-29
3428
3428
04-07
2809
2809

被折叠的 条评论
为什么被折叠?



