【问题标题】:How would I find a specific binary result in a binary source code?如何在二进制源代码中找到特定的二进制结果?
【发布时间】:2019-12-20 00:07:39
【问题描述】:

我正在尝试在给定的二进制代码字符串中查找二进制代码。其中搜索的是 N 代码(基于搜索参数的变量),而二进制代码的字符串是 M(基于搜索参数的变量)。

例如,我确定我需要在一个 20 位长的二进制字符串 110001011011010101010 中找到 0110。

然后我需要将我们找到的代码之后的剩余代码推送到另一个字符串值。

我太新了,我不能独自开始

int counter = 0;
string line;

Console.Write("Input your search text: ");
var text = Console.ReadLine();

System.IO.StreamReader file =
    new System.IO.StreamReader("SampleInput1.txt");

while ((line = file.ReadLine()) != null)
{
    if (line.Contains(text))
    {
        break;
    }

    counter++;
}

Console.WriteLine("Line number: {0}", counter);

file.Close();

Console.ReadLine();

我不确定如何让 sampleinput1.txt 工作或其他功能,但这似乎是一种可能的途径。

【问题讨论】:

  • 使用 IndexOf("0110")。然后你可以使用 SubString() 方法来获取剩下的数据。
  • 所以在您的示例中,您想将1101010101010101010 推送到另一个字符串值? (因为它包含 0110 两次)
  • 感谢 Jesse de Wit 的注意,我以为我小心不这样做,但答案将是它第一次出现。

标签: c# search binary indexof


【解决方案1】:

您可以通过获取搜索文本的索引然后使用该索引获取子字符串来找到该行的其余部分。请参阅下面的示例:

int lineNumber = 0;
string line;
Console.Write("Input your search text: ");
string text = Console.ReadLine();
string otherValue = null;
using (var reader = new StreamReader("SampleInput1.txt"))
{
    while ((line = file.ReadLine()) != null)
    {
        int index = line.IndexOf(text);
        if (index > -1)
        {
            otherValue = line.Substring(index);
            break;
        }

        lineNumber++;
    }
}

if (otherValue == null)
{
    Console.WriteLine("Not found");
}
else
{
    Console.WriteLine("Line number: {0}", lineNumber);
    Console.WriteLine("Other value: '{0}'", otherValue);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多