[英]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.