开发者

How to fetch the string using the substr with garbage characters?

开发者 https://www.devze.com 2023-03-14 17:20 出处:网络
I have this string, if size_t pos; pos = eNBSWVerTmp.find(\"MAC\"); When I print out the pos, pos 4294967295.

I have this string, if

size_t pos;
pos = eNBSWVerTmp.find("MAC");

When I print out the pos, pos 4294967295.

Is there a way I can 开发者_如何学JAVAfetch the string start from 1103?

♦ƒ+Hm.0_MAC_1103_064_00, 21.06.1ÿs7÷l ↕

I think garbage chars caused the pos out of range. Thanks in advance.


Do this:

std::string  test("MAC");
std::copy(test.begin(), test.end(), std::ostream_iterator<int>(std::cout," "));
std::cout << "\n";

std::copy(eNBSWVerTmp.begin(), eNBSWVerTmp.end(), std::ostream_iterator<int>(std::cout," "));
std::cout << "\n";

Now see if the numbers in the top line match a similar sequence of numbers in the bottom line.
I am betting you will not find a match. But you may be able to work out why.


If you start by

const size_t pos = eNBSWVerTmp.find("MAC");

you know that pos indicates where M in MAC can be found (or is std::string::npos if not found).

To get some substring starting, say, 4 bytes forward from pos, you can do

std::string   substring;

if (pos != std::string::npos)
    substring = eNBSWVerTmp.substr(pos + 4, number_of_bytes);

to get the number_of_bytes you want.


The position 4294967295 isn't a garbage number, it's consistent with what happens if you store a negative number in an unsigned type such as size_t. This would be returned by find if the substring MAC can't be found. So it probably means there's an invisible character inside the substring MAC. You might try iterating through the string in an encoding-specific way, and appending characters to a new string (or std::vector<char>) only if they're expected characters (i.e. letters, digits, etc.), then finding MAC within that.

0

精彩评论

暂无评论...
验证码 换一张
取 消