Convert bytes to bits in python

后端 未结 9 1686
轮回少年
轮回少年 2020-11-30 07:18

I am working with Python3.2. I need to take a hex stream as an input and parse it at bit-level. So I used

bytes.fromhex(input_str)

to convert t

9条回答
  •  囚心锁ツ
    2020-11-30 08:09

    Here how to do it using format()

    print "bin_signedDate : ", ''.join(format(x, '08b') for x in bytevector)
    

    It is important the 08b . That means it will be a maximum of 8 leading zeros be appended to complete a byte. If you don't specify this then the format will just have a variable bit length for each converted byte.

提交回复
热议问题