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

What is the correct way to convert bytes to a hex string in Python 3?

我正在处理 CryptoPals 挑战,我被困在挑战 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)

它当前 returns 所需的十六进制字符串,但作为字节对象而不是字符串。我如何转换这些?或者有更好的方法来处理这个问题吗?

Bytes 对象具有 "hex" 方法:

return b.hex()