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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -