我正在使用 Python 3.10,我正在嘗試讀取 Windows 可執行檔案,并將二進制資料以文本格式(1 和 0)輸出到文本檔案中。我只需要二進制,我不需要位元組的偏移量和 ASCII 表示。我不想要任何空格或新行。
total = ""
with open("input.exe", "rb") as f:
while (byte := f.read(1)):
total = total byte.decode("utf-8")
with open("output.txt", "w") as o:
o.write(total)
但是,它不起作用,出現以下錯誤。
Traceback (most recent call last):
File "converter.py", line 4, in <module>
total = total byte.decode("utf-8")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x90 in position 0: invalid start byte
uj5u.com熱心網友回復:
這段代碼應該做你想做的:
with open("input.exe", "rb") as f:
buf = f.read()
with open("output.txt", "w") as o:
binary = bin(int.from_bytes(buf, byteorder='big'))[2:] # or byteorder='little' as necessary
o.write(binary)
請注意,在處理非常大的檔案時,它可能會占用記憶體。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337864.html
上一篇:索引值不在串列中
