How can I read a file into bytes, and find a string of hex to match?

DropItLikeItsHot

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;
 }
mfatih

There's a more efficient way without reading into the whole file. I hope this way can help you.

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I match a string with a regex in Bash?

From Dev

How can I read the first two bytes of a .zip file to confirm that the appropriate magic number is present?

From Dev

How can i match this string?

From Dev

PHP string to hex/bytes?

From Dev

How can I read a set of bytes from a file and convert them to letters?

From Dev

How can I find a match and update it with RegEx?

From Dev

How can i find, match and replace end of line when there is a match

From Dev

How can I match this string '-,-,-,9,-'?

From Dev

How can I find the match string in pattern(AWK)?

From Dev

How can I convert a bytestring array in a hex string in Python 3.4?

From Dev

How can I pattern match for "any string"?

From Dev

How can i convert a QByteArray into a hex string?

From Dev

How can I find the best fuzzy string match?

From Dev

How can I properly read the sequence of bytes from a hyper::client::Request and print it to the console as a UTF-8 string?

From Dev

Read str from file contain hex bytes str character and decode?

From Dev

how can I convert my String (that represents hex values) to bytes?

From Dev

How to convert hex string to hex bytes format in PySpark

From Dev

Read str from file contain hex bytes str character and decode?

From Dev

How can I find files that are bigger/smaller than x bytes?

From Dev

in C# how do I convert what I think is a string of hex ascii to something i can read?

From Dev

How can I find a sequence of bytes in a pool of memory?

From Dev

How do I read a specific selection of bytes from a file type?

From Dev

How can I read an hex number with dlmread?

From Dev

Encode string as hex bytes

From Dev

How can I find out if a line is an exact match to a line in a text file?

From Dev

Python String to Hex Bytes

From Dev

How can i read bytes from a very heavy file? then store store them in a String e.g. .pdf .zip .xlsx files

From Dev

How to read bytes as string

From Dev

How can I find the new match position converting an Unicode String to a Byte String?

Related Related

  1. 1

    How can I match a string with a regex in Bash?

  2. 2

    How can I read the first two bytes of a .zip file to confirm that the appropriate magic number is present?

  3. 3

    How can i match this string?

  4. 4

    PHP string to hex/bytes?

  5. 5

    How can I read a set of bytes from a file and convert them to letters?

  6. 6

    How can I find a match and update it with RegEx?

  7. 7

    How can i find, match and replace end of line when there is a match

  8. 8

    How can I match this string '-,-,-,9,-'?

  9. 9

    How can I find the match string in pattern(AWK)?

  10. 10

    How can I convert a bytestring array in a hex string in Python 3.4?

  11. 11

    How can I pattern match for "any string"?

  12. 12

    How can i convert a QByteArray into a hex string?

  13. 13

    How can I find the best fuzzy string match?

  14. 14

    How can I properly read the sequence of bytes from a hyper::client::Request and print it to the console as a UTF-8 string?

  15. 15

    Read str from file contain hex bytes str character and decode?

  16. 16

    how can I convert my String (that represents hex values) to bytes?

  17. 17

    How to convert hex string to hex bytes format in PySpark

  18. 18

    Read str from file contain hex bytes str character and decode?

  19. 19

    How can I find files that are bigger/smaller than x bytes?

  20. 20

    in C# how do I convert what I think is a string of hex ascii to something i can read?

  21. 21

    How can I find a sequence of bytes in a pool of memory?

  22. 22

    How do I read a specific selection of bytes from a file type?

  23. 23

    How can I read an hex number with dlmread?

  24. 24

    Encode string as hex bytes

  25. 25

    How can I find out if a line is an exact match to a line in a text file?

  26. 26

    Python String to Hex Bytes

  27. 27

    How can i read bytes from a very heavy file? then store store them in a String e.g. .pdf .zip .xlsx files

  28. 28

    How to read bytes as string

  29. 29

    How can I find the new match position converting an Unicode String to a Byte String?

HotTag

Archive