代码之家  ›  专栏  ›  技术社区  ›  Ragnagard

在字符串中搜索十六进制子字符串

  •  1
  • Ragnagard  · 技术社区  · 16 年前

    4 回复  |  直到 16 年前
        1
  •  3
  •   Loki Astari    16 年前

    字符串的良好文档在这里:
    http://www.sgi.com/tech/stl/basic_string.html


    #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));
    }
    
        2
  •  2
  •   Satbir    16 年前

    c++String对象中有很多find选项,比如

    在字符串中查找内容(公共成员函数)

    查找字符串中最后一次出现的内容(公共成员函数)

    find_first_of

    find_last_of 从字符串末尾查找字符(公共成员函数)

    find_first_not_of

    find_last_not_of

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

        3
  •  0
  •   John D.    16 年前

    也许你可以试试 strstr() strtok 获取分隔符之间的值并自己解码。

        4
  •  0
  •   Patryk    10 年前

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

    记住, 0x00 为null,即字符串末尾。因此,如果您的源代码或搜索包含它们,您将找不到您要查找的内容,因为 strstr