title: C - Reverse Converter Process
body: <p><strong>Context:</strong></p>
  
  <p>I'm trying to reverse the process of a <code>c</code> converter. This is the <strong>original converter</strong>:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      FILE *f = fopen("input.bin", "rb");
      fseek(f, 0, SEEK_END);
      int s = ftell(f);
      rewind(f);
  
      FILE *o = fopen("output.txt", "wb");
  
      int i;
      for(i = 0; i &lt; s / 4; i++) {
          unsigned int u;
          fread(&amp;u, sizeof(u), 1, f);
          fprintf(o, "u32[%d] = 0x%08x;\n", i, u);
      }
  
      fclose(o);
      fclose(f);
  
      return 0;
  }
  </code></pre>
  
  <p>What it does is it creates a new file called <code>output.txt</code> and converts the <code>.bin</code> file to bytes in a order like this:</p>
  
  <pre><code>u32[0] = 0x53555441;
  u32[1] = 0x6373b848;
  u32[2] = 0x74654e65;
  u32[3] = 0x83486f53;
  u32[4] = 0x0ebe60ec;
  u32[5] = 0x48000000;
  u32[6] = 0x40245c8d;
  u32[7] = 0x24448948;
  u32[8] = 0xda894840;
  u32[9] = 0x244c8d48;
  u32[10] = 0x024fbf10;
  u32[11] = 0xc0310000;
  u32[12] = 0x482444c7;
  u32[13] = 0x74656b63;
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>Now, what I'm trying to do is the opposite. So, I'm trying to convert the output bytes of the above converter to a <code>.bin</code> file again, like its original state.</p>
  
  <p>I tried this, but I'm stuck on the loop part:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      FILE *f = fopen("loader.txt", "rb"); // INPUT
      fseek(f, 0, SEEK_END);
      int s = ftell(f);
      rewind(f);
  
      FILE *o = fopen("loader.bin", "wb"); // OUTPUT
  
      int i;
      for(i = 0; i &lt; s / 4; i++) {
          // grab u32 bytes from loader.txt and convert into a .bin
      }
  
      fclose(o);
      fclose(f);
  
      return 0;
  }
  </code></pre>
  
  <p>Can anyone help me out with this? Would be amazingly appriciated.</p>
  
  <hr>
  
  <p>Edit</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main(void) {
      FILE *f = fopen("loader.txt", "rb"); // INPUT
      rewind(f);
  
      FILE *o = fopen("loader.bin", "wb"); // OUTPUT
  
      int i;
      for(i = 0; i &lt; s / 4; i++) {
          fgets(f);
      sscanf(f);
      }
  
      fclose(o);
      fclose(f);
  
      return 0;
  }
  </code></pre>