简体   繁体   中英

In Python, how do I convert a .exe file to a string of 1s and 0s?

I seem to be able to find information on how to do this in C#, but not on how to perform the same operation in Python.

Any advice or suggestions would be appreciated. Thank you very much.

def padded_bin(number, width=8, padchar='0'):
    return bin(number)[2:].rjust(width, padchar)

with open(r'C:\path\to\file.txt', 'rb') as f:
    as_binary = ''.join(padded_bin(ord(c)) for c in f.read())
''.join((map(''.join, itertools.product(*['01']*8))[ord(c)]
         for c in open('foo').read()))
print "".join(bin(ord(c))[2:] for c in file("a.exe", "rb").read())

更新填充:

print "".join(("00000000"+bin(ord(c))[2:])[:8] for c in file("a.exe", "rb").read())

Use open with 'rb' as flags. That would read the file in binary mode

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM