where '\r' is the indicator that the string passed will be formatted by color

After running this code, the first <RGB> expression read well but on the following <RGB> format, the string erased not as expected. I got the result of:

"ABCD bla bla";

which is should be

"ABCDEFGH bla bla bla";

I want to erase only the <RGB:?,?,?> expression. Whats wrong in my code?
And also I need to pass first the std::stringstream(value) to an `int` before I put it into `unsigned char` because I get different value when passing it directly. How does std::stringstream(value) behave exactly? Or can you suggest much better string to int` converter?