How to split a CString in which there are no delimiters?

I have a CString that I want to split into small lines. This is a line consisting of a constant 2-byte header and a two-byte footer, but there is no distinguishable template in the rest of the line. I need to break them down based on sizes: so the first two bytes become the header, then I need to extract the next 2, then 3 and so on. (these numbers do not matter)

Example:

CString test = "1010eefabbccde1f1f"

I need

CString header = "1010";
CString test1  = "eefa";
CString test2  = "bbccde";
CString footer = "1f1f";

I read about sscanfused for this purpose, but I managed to use it only for split lines in int.

Example:

CString test = '101022223333331010';
int a,b,c,d;
sscanf(test,"%02d%02d%03d%02d",&a,&b,&c,&d); 

This works for strings containing only numbers. But when I do the same for strings, changing %dto %s, exceptions get.

Is there a better way to do this?

-1
1

, string test vector<size_t> sizes , , vector<int> result.

, test, . , .

- :

const auto test = "1010eefabbccde1f1f"s;
const vector<size_t> sizes { 4U, 4U, 6U, 4U };
const auto result = accumulate(cbegin(sizes), cend(sizes), vector<int>(), [&, pos = 0U](auto& a, const auto& b) mutable {
    a.push_back(stoi(test.substr(pos, b), nullptr, 16));
    pos += b;
    return a;
});

result :

4112
61178
12307678
7967

Live Example

0

Source: https://habr.com/ru/post/1679007/


All Articles