0

I have a corrupted *.jpg file from where I am trying to read the stored jpg. I can open the file in a Hex Editor (using http://mh-nexus.de/en/hxd/) and do copy-paste but I wanted to copy the data directly using c# instead. Can someone please help me in my quest??

Here is the exact data that I am looking for in the file: FF D8 FF DB

Example: for example

Deb
  • 737
  • 1
  • 5
  • 20
  • 2
    What do you want to do? Its not clear in the question, see if a file contains those bytes? Remove those bytes or substitute those bytes? Is this about removing copyright information from image files? – Jodrell Jun 16 '15 at 13:13
  • @Jodrell "FF D8 FF DB" marks the start of a jpeg image block – Deb Jun 16 '15 at 13:17
  • Duplicate of this ?? : http://stackoverflow.com/questions/283456/byte-array-pattern-search – PaulF Jun 16 '15 at 13:17
  • So, do you want to extract everything after and including `FF D8 FF DB`? – Jodrell Jun 16 '15 at 13:19

1 Answers1

2

The hex files you want to find are simply bytes. use the following:

byte[] toFind = new byte[]{0xFF, 0xD8, 0xFF, 0xDB};

Read the bytes:

byte[] fileContent = File.ReadAllBytes("your file");

now search the subarray in the array:

int loc = SearchBytes(fileContent,toFind);
if (loc != -1)
{
  //BINGO!
}

This is the code for 'SearchBytes' (from here):

static int SearchBytes( byte[] haystack, byte[] needle ) 
{
    var len = needle.Length;
    var limit = haystack.Length - len;
    for( var i = 0;  i <= limit;  i++ ) 
    {
        var k = 0;
        for( ;  k < len;  k++ ) 
        {
            if( needle[k] != haystack[i+k] ) break;
        }

        if( k == len ) return i;
    }

    return -1;
}
Community
  • 1
  • 1
Nissim
  • 6,395
  • 5
  • 49
  • 74