[Tutor] loop based on a binary number?

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Mar 12 20:27:22 EST 2004


> I am reading in binary data from an instrument using Python. The
second
> field in my data is supposed to represent the length (number of
bytes of
> binary data). Can I loop based on a binary? Or do I have to convert
it?

Assuming the number you read in is an integer not a string you can use
it directly. After all, all numbers in a computer are in binary.

You ae reading a bit stream and somehow chunking those bits into
pieces
depending on your protocol. Presumably your protocol says the first N
bits
do one thing and the next M bits represent the size. The question is
how
many bits and in what representation. If it's 8 or 16 or 32 or even 64
is important in determining the number. Even the order of the bytes,
is is big or little endian?

But assuming you have read the bits into the right format to represent
it as a number then you can use that number like any other:

def getInput(bits):
    you define this bit...

number = getInput(bitstream)
for n in range(number):
    yada, yada....

Alan G.




More information about the Tutor mailing list