-1

I want to decompile files into (/read them as) raw binary code. I haven´t found anything about that. Is it even possible? It must be in order for the computer to run it, but some OS may not allow to read it.

EDIT: Lets clear the answer. I want to start with (For example) a .txt file, and end with an array of 0's and 1's

AvidScifiReader
  • 279
  • 1
  • 3
  • 10
  • 7
    Erm. What? All files are stored as raw binary code. There is no "decompilation" to be done. – R. Martinho Fernandes Dec 06 '12 at 13:51
  • You mean read the assembly? Try `objdump -d` – Collin Dec 06 '12 at 13:52
  • Sounds like you need the Nabob compression tool! – Russ Freeman Dec 06 '12 at 14:01
  • Show us an example of a text file you want to "decompile". – John Dibling Dec 06 '12 at 14:50
  • An example? "Hello World" -> ("Decompilation") -> 0111010100101010... – AvidScifiReader Dec 06 '12 at 15:54
  • Then what did you mean when you said " in order for the computer to run it?" How would the computer "run" a text file that contained "Hello World?" – John Dibling Dec 06 '12 at 16:09
  • Everything in the machine is binary. "Text" is binary data that is meant to be interpreted as human-readable text according to some convention for how text should be represented binarily. "Decompilation" usually means the opposite (more or less) of what you ask: the translation of binary data into human-readable text, but is (almost) always only applied to executable code. Executable code is also binary data, but meant to be interpreted by a machine as instructions for it to execute according to some convention for how machine instructions should be represented binarily. – molbdnilo Dec 06 '12 at 16:51

1 Answers1

2

Files stored on the filesystems of operating systems like Windows and Linux don't need to be "decompiled" in the way you seem to think. They already are stored as binary data.

All that's left for you to do is simply read the file, but you need to use the right tool.

If you use something like stream I/O to read the file using the stream manipulators, you should consider that the stream manipulators are for formatted I/O. But you don't want formatted I/O, you want unformatted I/O.

Consider this example. If you have a file c:\temp\test.txt that contains:

Hello, world.

...and run this code:

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    ifstream f("c:\\temp\\test.txt", ios_base::binary|ios::in);
    for( char c = 0; f.get(c); )
    {
        cout << c << "\t" << "0x" << hex << static_cast<int>(c) << endl;
    }
}

You'll see the output is:

H       0x48
e       0x65
l       0x6c
l       0x6c
o       0x6f
,       0x2c
        0x20
w       0x77
o       0x6f
r       0x72
l       0x6c
d       0x64
.       0x2e
        0xd

        0xa

The hex numbers and the characters displayed are the save data, just formatted differently. There's no "decompilation" step or anything like that. All that's left to do at this point is store this data somewhere like a vector<char>, and maybe reformat the output so that it displays these values in binary rather than hex or ASCII. I'll leave that as an exercise to the reader.

John Dibling
  • 99,718
  • 31
  • 186
  • 324