# Reconstructing files from binary

Popular view tells us that any kind of information is just a collection of bits, that is zeroes and ones placed in a particular order. I was thus having this thought.

Suppose that I have some kind of file such as a text document, a PDF or anything else and that I also have a memory so perfect that I can remember the file exactly in its binary format, i.e. the number and place of zeroes and ones. But nothing else.

My question would then be if, in practice, it would be possible to reconstruct my initial file (without imposing any assumptions on what format it takes) from just this binary format.

Please be advised that I am not an expert in computer science and I do have only some very basic understanding. I do hope though this question fits here.

• If you remember the exact sequence, well of course. This is what the computer does. I don't understaqnd the question. "Reconstructing files from binary" does not make sense to me: all files are always binary, there is no other representation. – Raphael Apr 2 '13 at 7:56

First notice that a "file" is a sequence of 0s and 1s in binary format that are stored in some kind of persistent storage (an hard disk, a CD, an USB key, ...).

According to the the type of information stored in the file (a plain text, a PDF document, an image, a video, a song, ...) its bits (bytes) are usually arranged using standard rules and they have precise meaning: they follow a file format which specifies how information is encoded in the file.

Usually the name of the file (filename) has an extension (.mp3, .txt, .jpg, .pdf, .tex, ...) that indicates the file format i.e. how information is encoded.

Computer programs like text editors, image editors, video players, music players, ... can read a file in one (or more) format and decode the information stored in it (show the text on the screen, play it, process it, ... ).

Now suppose that you have a bunch of bytes in your head. First you can write them directly in a file using a program like a binary editor. When they are stored in a file, you need to discover its file format ...

1. first you can try to display it as a text file (like suggested by saadtaame in his answer): many file formats (text data, html files, xml files, ...) store the information in plain text (ASCII);

2. or you can change its extension (.pdf, .jpg, .mp3, .gif, .avi, ...), and try to open it with some standard programs ... if you are lucky you'll find one that opens it correctly;

3. or you can do something smarter. You can examine the first bytes of the file (header): they usually have a particular structure or contains specific fixed characters that allow you to quickly find the file format of the whole file.

For example the bytes 6,7,8,9 of a JPEG file are always 0x4a 0x46 0x49 0x46, which correspond to the string "JFIF" in ASCII. Windows PE executables contain the string "This program cannot ..." in the header. The first two bytes of a file in ZIP format are "PK", and so on ...

But there are thousands of file formats out there, so you can also consider using a computer program that given an unknown file can help you to find its file format automatically.

Finally don't forget that the information in a file can also be encrypted or the file format can be proprietary (and closed, i.e. undocumented); in these cases there are very little probability (zero?) to decode the information stored in the files.

Files are always stored in the disk in binary format. Therefore if you know the binary version of the file, you simply know the file itself.

A more interesting question is whether you can reconstruct the semantics of the file from its binary contents. Suppose you memorized the binary contents of a file, but forgot what type the file was. Could you reconstruct how to "read" the file?

If the file format is standard, the answer is yes. You could tell what type the file is in various ways - by looking at statistics, by considering the header, and more. Once we know what format the file is, we can "understand" it using the format specification.

I assume you understand binary. If every character is a byte (8 bits), you can recover the initial file (text) by translating every byte to a character. A simple algorithm for doing that is:

let initial_file be an empty text file
for every byte in binary file:
write character corresponding to byte in initial_file

• Thanks for the answer. Would it work for anything else than text? For example, could I recreate an executable in a similar way? – Kurt Mar 31 '13 at 19:06
• An executable is a binary file. You will have to provide more detail for a precise answer. – saadtaame Mar 31 '13 at 19:14