how to do bitwise exclusive or of two strings in python?

后端 未结 12 1797
攒了一身酷
攒了一身酷 2020-12-01 02:34

I would like to perform a bitwise exclusive or of two strings in python, but xor of strings are not allowed in python. How can I do it ?

12条回答
  •  悲&欢浪女
    2020-12-01 03:10

    For bytearrays you can directly use XOR:

    >>> b1 = bytearray("test123")
    >>> b2 = bytearray("321test")
    >>> b = bytearray(len(b1))
    >>> for i in range(len(b1)):
    ...   b[i] = b1[i] ^ b2[i]
    
    >>> b
    bytearray(b'GWB\x00TAG')
    

提交回复
热议问题