H,我正在尝试在 Python 中对两个字符串(应该先变成十六进制)进行异或。我知道一种方法会奏效:
def xor_two_str(str1, str2):
return hex(int(str1,16) ^ int(str2,16))
Run Code Online (Sandbox Code Playgroud)
但我试过这样的:
def change_to_be_hex(str):
return hex(int(str,base=16))
def xor_two_str(str1,str2):
a = change_to_be_hex(str1)
b = change_to_be_hex(str2)
return hex(a ^ b)
print xor_two_str("12ef","abcd")
Run Code Online (Sandbox Code Playgroud)
这将返回 TypeError: ^ 不应在 str, str 之间使用。我不知道为什么。
而且这个功能也不起作用:
bcd = change_to_be_hex("12ef")
def increment_hex(hex_n):
return hex_n + 1
result = increment_hex(bcd)
print result
Run Code Online (Sandbox Code Playgroud)
错误信息是:TypeError: cannot concatenate 'str' and 'int' objects 我觉得这很奇怪:(
谢谢!