Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there my beloved Community,

i'm currently trying to convert any file into a textfile consisting of it's own bits and bytes (e.g. any exe file to a string of 100010010010010010101...) and then reconverting them to a working file.

the conversion to a string seems to work, but somehow i don't get working files out of my already converted ones...
so i decided to ask you pros about it and present my methods, maybe there's something i didn't quiet get right.


this is the conversion to a string
C#
public static string ConvStep1(string path)
     {
         byte[] fileBytes = File.ReadAllBytes(path);
         string filenamewithoutextension = Path.GetFileNameWithoutExtension(path);
         string fileextension = Transform(Path.GetExtension(path).Replace(".", "")).ToLower();

         SaveToTextFile(ToBinaryString(fileBytes), filenamewithoutextension +"."+ fileextension + ".cry", path);
         File.Delete(path);
         return filenamewithoutextension +"."+ fileextension + ".cry";

     }


it uses this method for conversion

C#
public static string ToBinaryString(byte[] array)
      {
          var s = new StringBuilder();
          foreach (byte b in array)
              s.Append(Convert.ToString(b, 2));

          return s.ToString();
      }


and it uses this method to write the file:

C#
private static void SaveToTextFile(string data, string FileName, string sourcefile)
      {

              StreamWriter sw = new StreamWriter(FileName);

              sw.Write(data);
              sw.Close();
              sw.Dispose();


          File.Delete(sourcefile);
      }


and this is my re-conversion whith the bad outcome
just ignore the transform command as is only changes the extension of the file
C#
public static string ConvStep2(string path)
      {
          string withcryptedext = Path.GetFileNameWithoutExtension(path);
          string filenamewithoutextension = Path.GetFileNameWithoutExtension(withcryptedext);
          string fileextension = Transform(Path.GetExtension(withcryptedext).Replace(".", ""));
          StreamReader sr = new StreamReader(path);
          string a = "";
          while (!sr.EndOfStream)
          {
              a += sr.ReadLine();
          }
          sr.Close();
          sr.Dispose();
          SaveToExecFile(a, filenamewithoutextension +"."+ fileextension, path);
          return filenamewithoutextension +"."+ fileextension;
      }


as you can see, it reads the file line by line and then executes this method

C#
private static void SaveToExecFile(string data, string FileName, string sourcefile)
      {

          File.WriteAllBytes(FileName,FromBinaryString(data));


          File.Delete(sourcefile);
      }


C#
public static byte[] FromBinaryString(string s)
       {
           int count = s.Length / 8;
           var b = new byte[count];
           for (int i = 0; i < count; i++)
               b[i] = Convert.ToByte(s.Substring(i * 8, 8), 2);

           return b;
       }


as i already said, the output file doesn't work for some reason and consists only of gibberish.

what am i doing wrong?

thank you in advance for your help!
Posted
Comments
Patrice T 26-Jan-16 5:40am    
Advice: never do a conversion if you don't need it.

1 solution

Start by using the debugger.
Create a file which contains 4 bytes with hex values 00 FF 55 AA
Run your app, and look at the resulting file. It should be
00000000111111110101010110101010
And nothing else.
Now use that file as the input to your ConvStep2 method, and put a break point on the first line in FromBinaryString.
When execution reaches that point, it will stop and let you look at exactly what is going on.
What is in "s"? does it look like the file content above - if it doesn't, then why not?
If it does, then step through each line and check exactly what is being generated. It should be fairly easy to spot where something doesn't happen exactly as you expected - and that should tell you why it's a problem.

Give it a try: this is a skill called (predictably) debugging - and it's a lot easier to learn on a simple app like this than a massive 500,000 line monster with a problem! :laugh:
See what you can find out.
 
Share this answer
 
Comments
CPallini 26-Jan-16 4:55am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900