如何将文件读入字节,并找到要匹配的十六进制字符串



我想读取文件的内容并在数据中找到十六进制匹配项。我觉得使用"file.readallbytes"是矫枉过正的,因为我只需要逐个字节读取,直到找到十六进制匹配。是否有更好的替代方法,我可以改用,或者使用readallbytes对性能更好?我在下面所做的目前按原样工作。

我尝试读取的文件是一个简单的文本文件,里面有"hello"。

string match = "68656C6C6F";
foreach (var jsfile in jsscan)
{
    byte[] data = File.ReadAllBytes(jsfile);
    string dataString = String.Concat(data.Select(b => b.ToString("X2")));
    if (dataString.Contains (match))
    {
        MessageBox.Show(jsfile + dataString);
    }
}

由于mfatih更新了解决方案:

public void example()
{
    string match = "68656C6C6F"; //This is "hello" in hex
    byte[] matchBytes = StringToByteArray(match);

    foreach (var jsFile in jsscan)
    {
        using (var fs = new FileStream(jsFile, FileMode.Open))
        {
            int i = 0;
            int readByte;
            while ((readByte = fs.ReadByte()) != -1)
            {
                if (matchBytes[i] == readByte)
                {
                    i++;
                }
                else
                {
                    i = 0;
                }
                if (i == matchBytes.Length)
                {
                    Console.WriteLine("It found between {0} and {1}.", 
                       fs.Position - matchBytes.Length, fs.Position);
                    break;
                }
            }
       }
    }
}
public static byte[] StringToByteArray(String hex)
{
    int NumberChars = hex.Length;
    byte[] bytes = new byte[NumberChars / 2];
    for (int i = 0; i < NumberChars; i += 2)
            bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
    return bytes;
 }
有一种

更有效的方法,无需读取整个文件。希望这种方式能对你有所帮助。

string match = "68656C6C6F";
byte[] matchBytes = Encoding.ASCII.GetBytes(match);
foreach (var jsFile in jsscan)
{
    using (var fs = new FileStream(jsFile, FileMode.Open))
    {
        int i = 0;
        int readByte;
        while ((readByte = fs.ReadByte()) != -1)
        {
            if (matchBytes[i] == readByte)
            {
                i++;
            }
            else
            {
                i = 0;
            }
            if (i == matchBytes.Length)
            {
                Console.WriteLine("It found between {0} and {1}.", 
                       fs.Position - matchBytes.Length, fs.Position);
                break;
            }
        }
   }
}

最新更新