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

后端 未结 12 1798
攒了一身酷
攒了一身酷 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:29

    Below illustrates XORing string s with m, and then again to reverse the process:

    >>> s='hello, world'
    >>> m='markmarkmark'
    >>> s=''.join(chr(ord(a)^ord(b)) for a,b in zip(s,m))
    >>> s
    '\x05\x04\x1e\x07\x02MR\x1c\x02\x13\x1e\x0f'
    >>> s=''.join(chr(ord(a)^ord(b)) for a,b in zip(s,m))
    >>> s
    'hello, world'
    >>>
    

提交回复
热议问题