如何使用十六进制数字来解决^''str'和'str'''不支持的操作数类型?
让我们假设我有:如何使用十六进制数字来解决^''str'和'str'''不支持的操作数类型?
Plaintext= '53be6f50bf838a42b1ffda01ff64c162'
key= '000102030405060708090a0b0c0d0e0f'
在我的代码,我有一个功能叫做S盒,我必须在此行中sbox[ Plaintext[0]^Key[0]]
使用,但它给了我这个错误:
tempSbox = (sbox[Plaintext[0]^Key[0]])
TypeError: unsupported operand type(s) for ^: 'str' and 'str'
如何解决请问这个问题?
按位独占或运算符^
对整数有效。看到有您的字符串的十六进制表示,只需将它们转换(使用base
参数):您使用的是位运算符在两个字符串
tempSbox = (sbox[int(Plaintext[0], 16)^int(Key[0], 16)])
tempSbox = sbox[int(Plaintext[0],16)^int(Key[0],16)]
我想过,但我有一个十六进制数字:所以当我把 int()我有这个错误:int()与基数10无效的文字:'f' – Guillaume
@Guillaume - 看看[kendas评论](http://stackoverflow.com/questions/43610782/how-to-resolve-this-error-unsupported-operand-types-for-str-and-long#comment74270442_43610782)你也可以做转换到基地16太(默认值是基数10,这就是您看到该错误的原因)。 – Lix
重点不在于列表索引必须是整数,而是异或运算符使用整数。 – mkrieger1
- 试戴铸造'int'像这样:' sbox [int(Plaintext [0],16)^ int(Key [0],16)]' – Kendas
考虑让你的评论成为答案,Kendas。 – dmh