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 ?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can convert the characters to integers and xor those instead:
Here's an updated function in case you need a string as a result of the XOR:
See it working online: ideone
For bytearrays you can directly use XOR:
I've found that the ''.join(chr(ord(a)^ord(b)) for a,b in zip(s,m)) method is pretty slow. Instead, I've been doing this:
(Based on Mark Byers answer.)
If the strings are not even of equal length, you can use this