I want to read a file's content and find hex matches in the data. I feel like using "file.readallbytes" is overkill because I just need to read byte by byte until I find a hex match. Is there a better alternative I can use instead or is better for performance to use readallbytes? What I'm doing below currently works as is.
The file I am attempting to read is a simple text file, it has "hello" in it.
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);
}
}
Updated solution thanks to 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;
}