I want to parse some data with Python and scapy. Therefor I have to analyse single bits. But at the moment I have for example UDP packets with some payload like:
bytes = b'\x18\x00\x03\x61\xFF\xFF\x00\x05\x42\xFF\xFF\xFF\xFF'
Is there any elegant way to convert the bytes so that I can access single bits like:
bytes_as_bits = convert(bytes)
bit_at_index_42 = bytes_as_bits[42]
That will work:
def access_bit(data, num):
base = int(num // 8)
shift = int(num % 8)
return (data[base] >> shift) & 0x1
If you'd like to create a binary array you can use it like this:
[access_bit(data,i) for i in range(len(data)*8)]