-1

I have a CString that I want to break up into small strings. It is a string consisting of a constant 2 byte header and 2 byte footer, but the rest of the string has no discernible pattern. I need to break them up 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 are in no pattern either)

Example:

CString test = "1010eefabbccde1f1f"

I need

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

I read about sscanf being used for this purpose, but I have only managed to use it split strings into 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 by changing %d to %s, exceptions get raised.

Is there a better way to do this?

Michael Thomas
  • 443
  • 1
  • 4
  • 8
  • If there is no pattern that they are written with I can't see how you would ever know what to read. There has to be some sort of pattern. – NathanOliver Jun 09 '17 at 12:02
  • 4
    `"1010"` is 4 bytes, not 2. – melpomene Jun 09 '17 at 12:03
  • @NathanOliver I know that I have to separate the first 2, then the next 2, then the next 3 and so on. I know that pattern. Although this in itself isn't like a series of ascending numbers or something. I tried `GetAt(int index)` but VC++ did not like that at all. – Michael Thomas Jun 09 '17 at 12:06
  • @melpomene they are `hex` values. I should have mentioned that, sorry. – Michael Thomas Jun 09 '17 at 12:06
  • 3
    "%4x%4x%6x%4x" i think. use x for hex at least. you can always check the [manual](http://man7.org/linux/man-pages/man3/scanf.3.html) – sp2danny Jun 09 '17 at 12:11
  • Also be sure to use double quotes for strings, single quotes makes a character literal – sp2danny Jun 09 '17 at 12:13
  • 3
    Can you use `std::string` instead of `CString`? – NathanOliver Jun 09 '17 at 12:24
  • You say: "then I need to extract the next 2, then 3 and so on" is so on taken from counting numbers: 2, 3, 4, 5, 6... or is there some other pattern you're looking for? Or do you just want to split into header, 2, #, footer? – Jonathan Mee Jun 09 '17 at 13:33
  • @NathanOliver I could, but does it offer any advantage? – Michael Thomas Jun 10 '17 at 03:30
  • @JonathanMee No, they aren't part of the counting numbers. Each chunk represents a different kind of data, so the number of characters in each chunk is different. I know how the overall string is subdivided, it's just that this division has no pattern to simplify it. I need to explicitly state how to break up the whole string into chunks. – Michael Thomas Jun 10 '17 at 03:32
  • @MichaelThomas Well, `std::string` has iterators and a iterator constructor that make this pretty easy if you can use it. – NathanOliver Jun 10 '17 at 13:49

1 Answers1

0

My understanding is that given an input string test and vector<size_t> sizes of sizes, you wish to break the string apart into those sizes, and then you wish to take those parts, treat them as hex numbers, and return them in vector<int> result.

I'm going to presume that you have already tested test to ensure the correct number of characters exist. And I'm going to assume that the sizes include the header and footer sizes.

After running something like this:

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 will contain:

4112
61178
12307678
7967

Live Example

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288