1

I have an std:string. I would like to find the pattern "0x789c" in hexadecimal. According another thread in stackoverflow, i have done this :

int nPosD = buffer.find("\x78\x9C");

and tried

int nPosD = buffer.find(std::string("\x78\x9C"),2);

No success !The return is -1 !My pattern 0x789c is in my string ! I have tested it with HexEdit software.

Anyone have idea for this ?

Thanks a lot :)

best regards,

4
  • 2
    0x789c is 0x9c 0x78 in intel processors, not 0x78 0x9c
    – Daniel
    Feb 27, 2012 at 21:47
  • Should work, are you sure your hex editing software doesn't show your bytes in little endian (reverse byte order)? Feb 27, 2012 at 21:47
  • 1
    Your first mistake is to write a binary buffer to a string. A std::string’s purpose is to store text. If you have binary data, use a std::vector<unsigned char>. Feb 27, 2012 at 21:50
  • Yes, it was a little endian view in my Hex Editor !! Thanks Joachim Isaksson ! Feb 27, 2012 at 22:06

2 Answers 2

1
int nPosD = buffer.find(std::string("\x78\x9C"),2);

will look for the string "\x78\x9c" starting at the 3rd character (offset 2) in buffer. If it does not appear at or after that point, it will return -1. If you want to search the entire string from the beginning, get rid of the 2

0

The following works for me:

char data[] = {'A', 0x78, 0x9C};
string buffer(data, 3);
cout << buffer.find("\x78\x9C") << endl;

It prints "1" as it should. It works as well without the initial A. You have probably set your string wrong. It may contain '0x789c' in ascii instead of binary, in which case you want to do find("0x789c").

1
  • Yes, it was a little endian view in my Hex Editor !! Thanks Joachim Isaksson ! Feb 27, 2012 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.