Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python3中,将字节转换为十六进制字符串的正确方法是什么?_Python_Python 3.x_Cryptography_Xor - Fatal编程技术网

在Python3中,将字节转换为十六进制字符串的正确方法是什么?

在Python3中,将字节转换为十六进制字符串的正确方法是什么?,python,python-3.x,cryptography,xor,Python,Python 3.x,Cryptography,Xor,我正在进行加密伙伴挑战赛,我被困在挑战赛2第1组。我熟悉按位异或运算,但无法在十六进制字符串中获得所需的结果。这是我现在的职责: def strxor(s1, s2): bytes1 = unhexlify(s1) bytes2 = unhexlify(s2) b = b"" for c1, c2 in zip(bytes1, bytes2): b += bytes([c1^c2]) return hexlify(b) 它当前返回所需的十

我正在进行加密伙伴挑战赛,我被困在挑战赛2第1组。我熟悉按位异或运算,但无法在十六进制字符串中获得所需的结果。这是我现在的职责:

def strxor(s1, s2):
    bytes1 = unhexlify(s1)
    bytes2 = unhexlify(s2)
    b = b""
    for c1, c2 in zip(bytes1, bytes2):
        b += bytes([c1^c2])
    return hexlify(b)
它当前返回所需的十六进制字符串,但作为字节对象而不是字符串。我如何转换这些?还是有更好的方法来处理这个问题?

字节对象有“十六进制”方法:

return b.hex()