簡體   English   中英

在Python中,如何將.exe文件轉換為1和0的字符串?

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

我似乎能夠找到有關如何在C#中執行此操作的信息,但找不到有關如何在Python中執行相同操作的信息。

任何意見或建議,將不勝感激。 非常感謝你。

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())

使用帶有'rb'的open作為標志。 那將以二進制模式讀取文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM