0

This is the C# do-while loop where, r is a BinaryReader:

int data_len = 0, shift = 0;
byte b;
do
{
    b = r.ReadByte();
    data_len = data_len | ((b & 0x7F) << shift);
    shift += 7;
}
while ((b & 0x80) != 0);

This is what I came up with it fails with large sizes, so it is wrong

data_len = 1    # Because 1 byte is still required to store a zero
shift = 0
b = self.read_uint8()
while (b & 0x80) != 0:
    data_len |= (b & 0x7F) << shift
    shift += 7
    b = self.read_uint8()
return data_len

as well as this:

b = self.read_uint8()
data_len = b & 0x7F
shift = 0
    while (b & 0x80) != 0:
    b = self.read_uint8()
    data_len |= (b & 0x7F) << shift
    shift += 7
return data_len

Both do not work.

demberto
  • 489
  • 5
  • 15
  • 1
    Can you please add some sample data to show what's not working? – timgeb Sep 07 '21 at 09:30
  • 1
    A simple way to emulate a do-while loop with a while loop is to unroll the first iteration. You almost succeeded to do this in the second snipped, but you incorrectly initalized shift to 0 (it needs to be 7 after the first, unrolled loop) – Lesiak Sep 07 '21 at 09:39
  • Check out the answer here for how to emulate a do while loop: https://stackoverflow.com/questions/743164/how-to-emulate-a-do-while-loop – G Ritchie Sep 07 '21 at 09:39
  • @Lesiak I will try that thanks. – demberto Sep 07 '21 at 12:56
  • @timegb I am parsing FLP file events, its basically a varint algorithm going on – demberto Sep 07 '21 at 13:00

0 Answers0