This is a bit of a strange problem I have here. I'm working with C# 6 on the .NET platform on a binary compression algorithm. Multiple stages of compression are working great, even far better than expected! However, converting the unoptimized binary back into a file is proving to be a bit more of a headache than I expected.
The Case
Binary is being read from an arbitrary file, and passed along within the program as a string. Multiple waves of optimization work on the string, converting it into an intermediate representation, which is written as the compressed object. Then, deoptimization turns the intermediate form back into pure binary, ready to be written.
The Code
Binary Input
BinaryString = ""; Filename = filename;
StringBuilder sb = new StringBuilder();
foreach(byte b in File.ReadAllBytes(filename)) {
{
sb.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
}
BinaryString = sb.ToString();
This is how I'm accepting input. It will return a literal binary string, in the form of 11001010110001
The conversion from its intermeidate form returns exactly the same string.
Binary
Output Currently, I'm trying to directly write a binary file as bytes, as such:
List<Byte> bytes = new List<byte>();
foreach(char c in binary)
bytes.Add(Convert.ToByte(c));
File.WriteAllBytes(filename, bytes.ToArray());
The Problem
The method I'm trying right now for binary output is simply writing the binary outright to a text file, rather than writing a binary object to the filesystem.
We're compressing pictures, executables, text, git objects, etc. So it's obviously not feasible whatsoever to have it written like this.
Does there exist a method in C#/.NET that will easily let me translate the binary back into a file, or is this a more involved problem than I'm thinking?