I want to convert the received hex data into binary form. I get ValueError.
For example, I want the first value in the output to be printed as 0000.
received_data = " ".join("{:02x}".format(byte) for byte in (data))
print(received_data)
P_data = "{0:b}".format(received_data[0:1])
Output:
01 04 04
Error:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Try\test1\test2.py", line 22, in
<module>
P_data="{0:b}".format(received_data[0:1])
ValueError: Unknown format code 'b' for object of type 'str'
asked Nov 10, 2018 at 9:46
kn_pro
671 gold badge2 silver badges11 bronze badges
1 Answer 1
You should first convert your string into an integer
P_data = '{0:b}'.format(int(received_data[0:1], 16)).zfill(4)
answered Nov 10, 2018 at 9:59
mistiru
3,4364 gold badges16 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
lang-py
'{0:b}'.format(byte).zfill(8)? You said binary form not hex?