尝试将任何文件转换为位数组并将它们重新转换为C#中的工作文件 [英] Trying to Convert any File into an Array of Bits and reconverting them to a working File in C#

查看:111
本文介绍了尝试将任何文件转换为位数组并将它们重新转换为C#中的工作文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我心爱的社区,



我正在尝试将任何文件转换为由其自己的位和字节组成的文本文件(例如,任何exe文件到一串100010010010010010101 ...)然后将它们重新转换为工作文件。



转换为字符串似乎有效,但不知何故我不能正常工作我已经转换的文件中的文件...

所以我决定问你的专业人士并介绍我的方法,也许有些东西我没有安静得对。





这是转换为字符串

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

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

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:

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

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

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

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


          File.Delete(sourcefile);
      }







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!

推荐答案

首先使用调试器。

创建一个包含4个字节且带有十六进制值的文件00 FF 55 AA

运行您的应用程序,然后查看生成的文件。它应该是

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

还有别的。

现在使用该文件作为ConvStep2方法的输入,并在FromBinaryString的第一行放置一个断点。

当执行到达那一点时,它将停止并让你看看究竟发生了什么。

什么是在s?它看起来像上面的文件内容 - 如果没有,那么为什么不呢?

如果确实如此,那么逐步检查每一行并确切检查生成的内容。应该很容易发现某些事情并不像你预期的那样 - 这应该告诉你为什么这是一个问题。



试一试:这个是一种称为(可预测)调试的技能 - 在这样一个简单的应用程序上学习要比一个有问题的大型500,000行怪物更容易学习! :笑:

看看你能找到什么。

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.


这篇关于尝试将任何文件转换为位数组并将它们重新转换为C#中的工作文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆