[英]From binary file to int Python
我正在嘗試使用加密密鑰讀取二進制文件並將其加載到密碼表(始終為 64 字節到 8x8 表)。 試圖谷歌沒有結果。 我確信解決方案很簡單,但我找不到我做錯了什么。
錯誤:
自己。 cphtbl [i][j] = int.from_bytes(byte, byteorder = 'big')
類型錯誤:“int”對象不支持項目分配
我的代碼:
...
def createCipherTable(self):
keyFile = open(self.keyFilePath, "rb")
self.__cphtbl__ = [0,0,0,0,0,0,0,0] *8
for i in range(8):
for j in range(8):
byte = keyFile.read(1)
self.__cphtbl__[i][j] = int.from_bytes(byte, byteorder = 'big')
....
我也試過:
int(from_bytes(byte, byteorder = 'big')
但輸出是:
NameError: name 'from_bytes' is not defined
還試過:
self.__cphtbl__[i][j].from_bytes(byte, byteorder = 'big')
但后來它說:
類型錯誤:“int”對象不可下標
的問題是, self.__cphtbl__
是一個1d
數組,而不是2d
。
如果你想要2d
數組,請執行以下操作:
...
def createCipherTable(self):
keyFile = open(self.keyFilePath, "rb")
self.__cphtbl__ = [[0,0,0,0,0,0,0,0]] * 8 # Here is change [[]] * n instead of [] * n
for i in range(8):
for j in range(8):
byte = keyFile.read(1)
self.__cphtbl__[i][j] = int.from_bytes(byte, byteorder = 'big')
....
所以問題是你的__cphtbl__
不是一個二維數組,你正試圖
cphtbl[i][j] = int.from_bytes(b'\\x00\\x10', byteorder = 'big')
因此錯誤。 您需要創建一個二維數組,它將按預期工作
要創建一個二維數組,你可以簡單地做 -
[[0,0,0,0,0,0,0,0]] *8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.