Using Python How can I read the bits in a byte?

前端 未结 9 534
礼貌的吻别
礼貌的吻别 2020-12-05 06:55

I have a file where the first byte contains encoded information. In Matlab I can read the byte bit by bit with var = fread(file, 8, \'ubit1\'), and then retrie

9条回答
  •  一向
    一向 (楼主)
    2020-12-05 07:14

    With numpy it is easy like this:

    Bytes = numpy.fromfile(filename, dtype = "uint8")
    Bits = numpy.unpackbits(Bytes)
    

    More info here:
    http://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html

提交回复
热议问题