Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- I'm debugging a code written by another developer. I have a bitstream of data of different sizes which is written to a binary file as follows:
- bitstream.add(0x01, 8)
- bitstream.add(0x01, 32) # write data unit dummy size
- par_id, ps_id = 16
- bitstream.add(par_id, 12)
- bitstream.add(ps_id, 12)
- where the `add` function is the following:
- def add(self, data, length):
- s = ''
- if (type(data) == str):
- for char in data:
- b = bin(ord(char))[2:]
- s = s + "{:0>8}".format(b)
- else:
- s = bin(data)[2:]
- if (len(s) < length):
- resto = length - len(s)
- for _ in range(0, resto):
- s = '0' + s
- s = s[0:length]
- self.cache = self.cache + s
- self.flush()
- The bitstream then contains
- - 8 bits -> value 0x01
- - 32 bits -> value 0x01
- - 12 bits -> value 16
- - 12 bits -> value 16
- I have some issues in decoding the bitstream
- # unpack first 1 byte/8 bits
- data1, = struct.unpack('>B', in_file.read(1)) # value 1
- # unpack 2 bytes/16 bits
- number_tmp = struct.unpack('>H', in_file.read(2))[0] # value 16
- # set the 16th bit in number_tmp
- data2 = (data1 << 16) | number_tmp # value 65552
- # get last 5 bits in data2
- ps.parent_id = data2 >> 12 # value 16
- # bit mask 12 bits
- ps.id = data2 & 0x000FFF # value 16
- What it is not clear to me:
- 1. **data1** contains the first 8 bits and its value is 1 **OK**
- 2. **number_tmp** contains the following 16 bits, but its value is not 1 **NOT OK**
- **OBS**: I'm expecting to read the next 32 bits of the bitstream flow described above, instead it appears these 32 bits are ignored, since `number_tmp` evaluates to 16.
- 3. **data2** is needed to set the 16th bit of `number_tmp`. I'm totally new to this kind of operations, so it is not really clear what is the purpose.
- 4. **ps.parent_id** is set to the decimal value of the last 5 bits of `data2`
- 5. **ps.id** is set to the decimal value of the first 5 bits of `data2`
- Could anyone put light in my issues? I've read some tutorial but I could not find a proper explanation of what happens in my code.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement