So basically I'm writing an application that looks for PNG files in a binary file. It does this by reading in an entire binary in file into a byte array and then converting it to a string using the Convert.ToBase64String method and then using a regex that matches a PNG's header information and end chunk to find the images. Problem is using the ToBase64String method generates wildly different outputs depending on the length of the byte array and the documentation on MSDN doesn't seem to elaborate on it. Anyways here's an example of what I mean.
byte[] somebytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
Console.WriteLine(Convert.ToBase64String(somebytes));
The output in this case is "AQIDBAUGBwg=" now if I skip a byte...
byte[] somebytes = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
somebytes = somebytes.Skip(1).ToArray();
Console.WriteLine(Convert.ToBase64String(somebytes));
The output is now "AgMEBQYHCA==" so almost every character has changed from the previous example.
So am I hopelessly following the wrong path here for regexing a binary file or is there a method (maybe by padding?) I can guarantee more consistency across these conversion?
Update: Based on the feedback I've gathered it seems I should just move away from the Regex solution and manually search for the start and end byte sequences manually myself. Not sure why I'm being downvoted as I just wanted to understand why my other solution did work and there doesn't seem to be any other posts on this topic. Anyways thanks everyone for the quick feedback. I'll post the algorithm I used for finding images when I'm done in case it might benefit someone else.