2

I have a matlab script that loads values by reading non-standard length bits.

For example:

[file, errmsg] = fopen(FileName,'r');

year = fread(file,1,'ubit12','ieee-le'); 
month = fread(file,1,'ubit4','ieee-le'); 
day = fread(file,1,'ubit5','ieee-le'); 
version = fread(file,1,'ubit11','ieee-le');

The values loaded are:

year =2016
month =7
day =21
version =0

While the binary version of the file is:

11100000 01110111 00010101 00000000

(as retrieved by doing '{0:08b}'.format(ord(byte)) in python)

In other words,

year -> 4 lsb of byte 2 + byte 1 (011111100000)
month -> 4 msb of byte 2 (0111)
day -> 5 lsb of byte 3 (10101)
version -> 3 msb of byte 3 + byte 4 (00000000000)

Is there a pythonic way to achieve the same functionality?

dr.doom
  • 480
  • 1
  • 6
  • 17

1 Answers1

0

Bit array I think this what you need.

Amjad
  • 3,110
  • 2
  • 20
  • 19