[Tutor] How to convert binary files back to text files?

Alan Gauld alan.gauld at btinternet.com
Sun Aug 30 01:29:02 CEST 2009


"prasad rao" <prasadaraon50 at gmail.com> wrote

> I want to convert compiled files to text files.
> I believe compiled files are binary files.

If you are talking about compiled executables made from C++ for 
example yes. But...

> I am able to convert a given string  to binary and back to text.

They are not just binary representations of the source code 
thay are translations of the C++ instruictions into the native 
binary codes of the target computer plus a whole bunch of 
housekeeping binary code that sets up the runtime environment.

Decompiling a binary back to source code is incredibly difficult 
and even when done right the resulting code is barely recognisable 
compared to the original - for example a for loop may come out 
as a while loop - especially in C++ - and all variable names may 
be totally changed because the bninary doesn't need them (it uses 
memory addresses not names)

So if thats what you are aiming for it won;t ever work. And a lot 
of software companies will be very happy that it won;t or lots of 
people would be stealing their proprietary and copyright code!

> But when it comes to a file it is proving to be impossible.

Yes, see above...

> def p(a):
>    s=open(a,'rb')
>    for x in s:

I doubt this will work for a binary file, you usually have to 
use read() and provide a buffer size.

>        d=map(lambda y:chr(int(y,2)),x)

This is assuming that the binary digits are all stored in 
individual lines, which is not the case, they are stored 
consecutively with no breaks, thats wgy you need to use 
read() and tell it how many bytes you want to read.

What you appear to be doing is the equivalent to 

od -a

in Unix - ie dump a file in its ascii representation.

It would be instructive for you to try that command if you have 
access to a Unix box... Try it on a text file then try it on an 
executable file then try it on a compiled Pyhthon module.

Finally try using the dis module in Python to look at 
the disassembled pcode - or just read the documentation 
for dis... 

>>>> p("C:/pp.txt")

OK, Now you appear to be examining a tet file as if it were binary, 
thats different again - and slightly more doable.

Could you explain what exacvtly you are trying to do - and why?
Maybe we can help?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list