【问题标题】:Search Hex substring in string在字符串中搜索十六进制子字符串
【发布时间】:2010-12-02 01:13:12
【问题描述】:

好吧,我得到了一个接收二进制数据的套接字,并且我将该数据放入一个字符串中,其中也包含值和字符串值。 (例如“0x04,h,o,m,e,....”)

如何在该字符串中搜索十六进制子字符串?

即我想搜索“0x02,0x00,0x01,0x04”。

我要的是 c++ 版本的 python 'fooString.find("\x02\x00\x01\x04")'

谢谢大家:)

【问题讨论】:

    标签: c++ string find hex substring


    【解决方案1】:

    或许你可以试试strstr() 或相关功能。或者您可以使用 strtok 之类的东西来获取分隔符之间的值并自行解码。

    【讨论】:

    • 问题在于 strstr() 和其他 c 字符串函数适用于 nul 终止的 c 字符串,因此不适用于 OP 的示例搜索字符串。
    • 他说他正在将数据从他的流中提取到一个他想要搜索的字符串中。我认为他在进行转换时是空终止的。
    • strtok() 是个坏主意,因为它修改了基础数据以跟踪其当前位置。
    • @John D. 问题不在于向字符串添加空终止符,而在于要搜索的字符串是第二个位置为 0x00 的二进制字符串,并且输入也是二进制的,并且可以在字符串中的任何位置包含 nuls。这样的字符串不能用 c 风格的字符串处理(因此用 strstr()),你需要一个 c++ string:: 然后它可以使用这个类的各种搜索方法执行所需的任务。
    【解决方案2】:

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

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

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

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

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

    find_first_not_of 查找字符串中缺少的字符

    find_last_not_of从末尾开始查找字符串中缺少的字符(公共成员函数)

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

    转到上面的链接,看看哪个适合你

    【讨论】:

    • Find 几乎完全符合我的需求,但是如何将十六进制值列表转换为字符串以作为参数传递给“find”?谢谢
    • 使用 stringstream 来构造你的输入参数
    • 对于您要搜索的文字字符串,使用与 Python 中相同的语法: string sSearchPattern = "\x02\x00\x01\x04"; (请注意,python 字符串比 c 字符串更类似于 C++ 字符串。后者是 nul 终止的,因此不能在字符串中包含任何 nul 字符。
    【解决方案3】:

    试试这样的:

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

    请记住,0x00 为空,即字符串的结尾。因此,如果您的来源或搜索包含它们,您将找不到您要查找的内容,因为strstr 将在第一个 null 处停止。

    【讨论】:

      【解决方案4】:

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

      十六进制标记的传递就像 Python 一样(您认为 Python 是从哪里获得语法的)。
      字符 \x??是单个十六进制字符。

      #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));
      }
      

      【讨论】:

      • 这里唯一的小问题是创建一个包含空字符的 std::string 实例,用于 Ragnagards 示例字符串。为此,您需要带有字符数组和长度的 std::string 构造函数... size_t pos = fooString.find(std::string ("\x02\x00\x01\x04", 4));
      猜你喜欢
      • 1970-01-01
      • 2014-03-07
      • 2010-10-04
      • 2019-07-27
      • 2017-03-22
      • 2016-02-26
      • 2017-03-04
      • 1970-01-01
      • 2011-10-22
      相关资源
      最近更新 更多