Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在文件中搜索十六进制代码_C#_File_Search_Hex - Fatal编程技术网

C# 在文件中搜索十六进制代码

C# 在文件中搜索十六进制代码,c#,file,search,hex,C#,File,Search,Hex,我想做一个程序,允许用户在文件中搜索特定的十六进制代码,输出将是偏移量或未找到。 到目前为止,我掌握的代码是: namespace search { class Program { static void Main(string[] args) { System.IO.BinaryWriter bw = new BinaryWriter(File.OpenWrite("C:\\1.txt")); bw.BaseStream.Position =

我想做一个程序,允许用户在文件中搜索特定的十六进制代码,输出将是偏移量或未找到。 到目前为止,我掌握的代码是:

 namespace search
 {
class Program
{
    static void Main(string[] args)
    {
        System.IO.BinaryWriter bw = new BinaryWriter(File.OpenWrite("C:\\1.txt"));
        bw.BaseStream.Position = 3;
        bw.Write((byte)0x01);
        bw.Close();
        Console.WriteLine("Wrote the byte 01 at offset 3!");
    }
}
}

我在网上到处都找过了,没有找到任何有用的东西,可以搜索十六进制代码并输出偏移量吗

编辑1:

假设我们有这个文件1.txt,在这个偏移量0x1300,我们有这个十六进制代码0120/0x01 0x20/0120,我不知道怎么写。打开程序后,它会用console.readline询问您要搜索的十六进制代码,输出为0x1300

编辑2: 我的问题与此类似
它有一个解决方案,但在vb.net中,它使用BinaryReader查找写入文件的字节

//Write the byte
BinaryWriter bw = new BinaryWriter(File.OpenWrite("1.txt"));
bw.BaseStream.Position = 3;
bw.Write((byte)0x01);
bw.Close();
Console.WriteLine("Wrote the byte 01 at offset 3!");

//Find the byte
BinaryReader br = new BinaryReader(File.OpenRead("1.txt"));
for (int i = 0; i <= br.BaseStream.Length; i++)
{
     if (br.BaseStream.ReadByte() == (byte)0x01)
     {
          Console.WriteLine("Found the byte 01 at offset " + i);
          break;
     }
}
br.Close();

你也看过BinaryReader类吗?十六进制代码,是指字节吗?@TimS。我不是很确定,在我做了所有的搜索之后,我感到困惑。假设我有一个名为1.txt的文件,它在偏移量0x1300处有这个十六进制代码0120,我想用这个程序找到它。@RowlandShaw是的,我看过这篇论文,但它只将十六进制代码转换成任何值,但我需要偏移量。所以试着读取你的文件,一次一个字节,直到找到正确的字节为止。知道如何搜索大于01的字节吗?类似于0x01 0x20 0x13