johnnym
johnnym

Reputation: 35

How do I XOR a block of bytes read from a binary file in Python?

I am reading a block of bytes from a binary file using file.read(block_size). The variable that these bytes go into is typed <class 'bytes'>.

I need to XOR each byte in the stream with single byte value expressed in Hex, e.g. \x0A.

What is the operator in Python for doing this?

Upvotes: 0

Views: 205

Answers (1)

Adid
Adid

Reputation: 1584

Python's bitwise-XOR operator is ^

>>> 15^8
7

edit: The XOR operator works with hex numbers as well:

>>> 0xA^8
2

Upvotes: 0

Related Questions