'Replace multiple bytes in bytearray

How can I replace multiple bytes in a bytearray? For example:

b"\x00\x01\x02\x03\x04\x05"

I want to replace \x02\x03 with \xFF\xFF and \x04\x05 with \xEE\xEE. How can I do this all at once?



Solution 1:[1]

The replace method can also be used on byte object in python

a = b"\x00\x01\x02\x03\x04\x05"
b = a.replace(b"\x02\x03", b"\xFF\xFF").replace(b"\x04\x05", b"\xEE\xEE")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Xiidref