views:

574

answers:

4

Hi all!

Well i got a socket that receives binary data and I got that data into an string, containing values and strings values too. (for example "0x04,h,o,m,e,....")

How can i search for an hex substring into that string?

I.e. i want to search "0x02,0x00,0x01,0x04".

I'm asking for a c++ version of python 'fooString.find("\x02\x00\x01\x04")'

Thanks to all :)

A: 

Perhaps you can try strstr() or a related function. Or you can use something like strtok to grab the values between your delimiters and decode it yourself.

John D.
The problem is that strstr() and other c string functions work on c strings which are nul terminated, and therefore do not work with the OP's sample search string.
mjv
He said he was pulling the data from his stream into a string within which he was wanting to search. I assumed he was null terminating as he made that conversion.
John D.
strtok() is a bad idea as it modified the underlying data to keep track of its current location.
Martin York
@John D. The problem is not so much the addition of a null terminator to the string, but the fact that the string to search for is a binary string which has 0x00 in second position, and the input is also binary and may include nuls anywhere in the string. Such strings cannot be handled with c-style strings (and hence with strstr()), you need a c++ string:: which can then perform the needed task with the various search methods of this class.
mjv
+2  A: 

There are a lot of find options in c++ String object , Like

find Find content in string (public member function)

rfind Find last occurrence of content in string (public member function)

find_first_of Find character in string (public member function)

find_last_of Find character in string from the end (public member function)

find_first_not_of Find absence of character in string

find_last_not_of Find absence of character in string from the end (public member function)

http://www.cplusplus.com/reference/string/string/

Go to above link and see which suits you

sat
Find almost match my needs perfectly but how can i turn the hex values list into an string to pass as param to "find"?thanks
Ragnagard
use stringstream to construct your input param
sat
and as for the literal string you are searching for, use the very same syntax as in Python: string sSearchPattern = "\x02\x00\x01\x04"; (note that python strings are more akin to C++ strings than c strings. The latter are nul-terminated and hence cannot contain any nul character within the string proper.
mjv
A: 

try something like this

char find[] = {0x02, 0x04, 0x04, 0x00}; int pos = strstr(inputStr, find);

Just remember, 0x00 is null, i.e. end of string. So if your source contains them, or your search, you wont find what your looking for because strstr will stop at the first null.

keick
+2  A: 

Good documentation for string is here:
http://www.sgi.com/tech/stl/basic%5Fstring.html

Hex tokens are passed just like Python (Where do you think Python got the syntax from).
The character \x?? is a single hex character.

#include <iostream>
#include <string>


int main()
{
    std::cout << (int)'a' << "\n";
    std::string             x("ABCDEFGHIJKLMNOPabcdefghijklmnop");
    std::string::size_type  f   = x.find("\x61\x62");   // ab


    std::cout << x.substr(f);

    // As pointed out by Steve below.
    //
    // The string for find is a C-String and thus putting a \0x00 in the middle
    // May cause problems. To get around this you need to use a C++ std::string
    // as the value to find (as these can contain the null character.
    // But you run into the problem of constructing a std::string with a null
    //
    // std::string  find("\0x61\0x00\0x62"); // FAIL the string is treated like a C-String when constructing find.
    // std::string  find("\0x61\0x00\0x62",3); // GOOD. Treated like an array.

    std::string::size_type f2 = x.find(std::string("\0x61\0x00\0x62",3));
}
Martin York
The only minor issue here is creating an std::string instance that includes a null character, for Ragnagards example string. To do that, you need the std::string constructor that takes a character array and a length...size_t pos = fooString.find(std::string ("\x02\x00\x01\x04", 4));
Steve314
your answers fullfill my request, thank ya both :D
Ragnagard