0

I apologies if this question has been answered in another post but I can't seem to figure this out. What I'm trying to do is take a directory path that I have that is selected through FolderBrowserDialog. The folder that that I select is then processed and then displays all of the files within the folder selected and the files in the subDirectory. All that is done... What I want to do is take the files within the folder selected and compare it to the hex of the file to only show files such as PDF or JPG. I saw a few things that say that I need to convert to binary which I believe I have done. Then convert that Binary to hex. So I need to figure out how to take the binary and convert it to a hex from the directory path that is chosen. here is a sample of code of what I have

// Insert logic for processing found files here.

    public static void ProcessFile(string path)
    {

        byte[] fileBytes = File.ReadAllBytes(path);
        StringBuilder sb = new StringBuilder();

        foreach (byte b in fileBytes)
        {
            sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
        }


        Console.WriteLine("Test {0}",sb);
        Console.ReadLine();
    }

Like I said, I believe that I have the code to convert to binary correct, it just turns the entire directory path to binary which I'd prefer to just be the file and not the whole path. I can't seem to find out how to successfully convert the binary to hex so that I can exclude files other than PDF and JPG. Sorry for this being so long.

Edit: I need to compare the file signature of a file and compare it to JPG which starts with 0xFFD8 and PDF which starts with 0x25504446. I need to determine whether or not a file is PDF or JPG using those values. If I got my vocab wrong I apologize. If anyone has a better idea on how I should go about comparing these two using 0x25504446 and 0xFFD8 I'm all ears. Sorry if I worded any of the description weirdly.

ChadGH
  • 11
  • 1
  • 1
    1) Open read. 2) Read the number of bytes you need. 3) Compare that byte sequence to the expected byte sequence. Don't even bother converting to hex - why add an extra step? – ProgrammingLlama Feb 12 '19 at 06:29
  • Why is the need of converting to hex and then filtering the files based on their extension. – Usama Kiyani Feb 12 '19 at 06:29
  • @Usama I don't think OP is planning on filtering by extension, other than as a preliminary filter. – ProgrammingLlama Feb 12 '19 at 06:31
  • Have a look at this post: https://stackoverflow.com/questions/1886866/how-to-find-the-extension-of-a-file – Mike Feb 12 '19 at 06:32
  • @Jhon that what I am asking there is no need to convert to hex. – Usama Kiyani Feb 12 '19 at 06:42
  • I need to compare the file signature of a file and compare it to JPG which starts with 0xFFD8 and PDF which starts with 0x25504446. I need to determine whether or not a file is PDF or JPG using those values. If I got my vocab wrong I apologize. If anyone has a better idea on how I should go about comparing these two using 0x25504446 and 0xFFD8 I'm all ears. – ChadGH Feb 12 '19 at 12:30
  • Can't you just check the file extension? – Rosdi Kasim Feb 12 '19 at 12:40
  • I understand that's an option but I can't use that due to some files being saved with a different ext but actually being a different file type. Plus I have to do it comparing with the value that a recently posted. I got everything else completed that I need done, I have never worked with Hex so this is all very confusing to me I would like nothing more then to just compare the ext and be finished my code but it is asking for me to compare to 0xFFD8 and 0x25504446 – ChadGH Feb 12 '19 at 12:47
  • Are you allowed to use library? This might help : https://github.com/rocketRobin/myrmec – Rosdi Kasim Feb 12 '19 at 12:55
  • @John do you have a link or sample that I could use to try what you are suggesting, I have found some samples but they seem all over the place and I'm struggling to make sense of it. – ChadGH Feb 12 '19 at 14:36
  • @ChadGH Rosdi's suggestion looks like it might be the ticket. – ProgrammingLlama Feb 13 '19 at 00:27

0 Answers0