c++ - How can I determine the length of a std::string which contains "\0" in its middle? -
i writing c++ application receives input socket. input comes rawdata string , so, contains lots of "\0" in body.
my question is, how can determine length/size without stoping @ "\0"s, able read complete socket response? response itsef 148 long, size() retuns 2.
size()
returns actual length of string , not consider null bytes end string. reason you're getting size of 2 because there different ways set string's value , of them do consider null bytes string terminator. you've mistakenly set value of string string of 2 characters rather set hold complete response data, , size()
accurately reflecting mistake.
the solution initialize or set string using method not consider null bytes:
char buffer[] = "abcd\0efg"; std::string s(buffer, 8); std::cout << s.size() << '\n'; // outputs "8" std::string s2(buffer); std::cout << s2.size() << '\n'; // outputs "4"
Comments
Post a Comment