Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top