我正在使用Python3为AES编写一些代码,我想将字符串转换为十六进制
我的代码
PlaintextString = "secret message!"
CipherKeyString = "password is here"
PlaintextArray = []
CipherKeyArray = []
while len(PlaintextArray) < IOBlockSize: PlaintextArray.append(0)
while len(CipherKeyArray) < CipherKeySize: CipherKeyArray.append(0)
for i in range(len(list(PlaintextString))): PlaintextArray[i] = int(list(PlaintextString)[i].encode("hex"), 16) # 16 stands for HEX
for i in range(len(list(CipherKeyString))): CipherKeyArray[i] = int(list(CipherKeyString)[i].encode("hex"), 16) # 16 stands for HEX
但我犯了这个错误
LookupError: 'hex' is not a text encoding; use codecs.encode() to handle arbitrary codecs
请帮帮我
多谢各位
结论
使用UTF-8作为文本编码,并将字符串转换为字节,而不是列表
可以使用下标将任何字节读取为整数
解释
要将字符串转换为十六进制(二进制),必须确定字符串的文本编码。例如,通常使用UTF-8作为文本编码将Unicode字符转换为二进制
在python3中,使用
STRING.encode(ENCODING)
将字符串转换为字节前缀
b
表示值是字节,而不是字符串转换为字节后,可以将任何字节读取为下标为
BYTES[INDEX]
的整数。其他类似字符串的切片操作也可用于字节相关问题 更多 >
编程相关推荐