search - Workin with binary in python -
is there easy way work in binary python?
i have file of data receiving (in 1's , 0's) , scan through , patterns in binary. has in binary because due system, might off 1 bit or throw off when converting hex or ascii.
for example, open file, search '0001101010111100110' or string of binary , have tell me whether or not exists in file, is, etc.
is doable or better off working language?
to convert byte string string of '0' , '1', can use one-liner:
bin_str = ''.join(bin(0x100 + ord(b))[-8:] b in byte_str)
combine opening , reading file:
with open(filename, 'rb') f: byte_str = f.read()
now it's simple string search:
if '0001101010111100110' in bin_str:
Comments
Post a Comment