我有两个字符串值,其中包含char和hex等效值。如何对两个字符串进行异或运算,结果应采用此格式“\ x00 \ x01 \ x ...”
其中: String1和String2与相应的ASCII值相等
String1 = "abcd"
String2 = "\x61\x62\x63\x64"
String3 = String1 ^ String2
输入:
String1 = "ABC"
String2 = "\x72\x61\x74"
我正在使用以下方法查找XOR值
hex(ord('A')) ^ '\x72' = ??
hex(ord('B')) ^ '\x61' = ??
hex(ord('C')) ^ '\x74' = ??
答案 0 :(得分:0)
我认为您的想法是:
String1 = "ABC"
String2 = "\x72\x61\x74"
result = "".join(chr(c1 ^ c2) for c1, c2 in zip(map(ord, String1), map(ord, String2)))
print(result)
# prints "3#7"
或者,如果要打印十六进制表示,可能是:
String1 = "ABC"
String2 = "\x72\x61\x74"
result = "".join("\\x{:02x}".format(c1 ^ c2) for c1, c2 in zip(map(ord, String1), map(ord, String2)))
print(result)
# prints "\x33\x23\x37"