python: hex to bin with binascii.a2b_hex results in binascii.Error: Odd-length string

110 Views Asked by At

i am trying to convert a hex string to bin using binascii.a2b_hex but i get binascii.Error: Odd-length string only with some strings, not everytime.

for example this is the string throwing the error: 177B16283F6C72F52DB9F00DF2629EB6F925A67AEF85A93F5588C62DCDB0050

if i try to do binascii.a2b_hex('177B16283F6C72F52DB9F00DF2629EB6F925A67AEF85A93F5588C62DCDB0050') i get:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
binascii.Error: Odd-length string

just in case: i get this string converting in hex a string of bytes using:

"{0:0>4X}".format(int('0000000101110111101100010110001010000011111101101100011100101111010100101101101110011111000000001101111100100110001010011110101101101111100100100101101001100111101011101111100001011010100100111111010101011000100011000110001011011100110110110000000001010000',2))

i am not having this problem with other strings of bits different from this one

1

There are 1 best solutions below

0
On

string length is 63 instead of 64, because of missing leading zeroes. To fix this

binascii.a2b_hex('177B16283F6C72F52DB9F00DF2629EB6F925A67AEF85A93F5588C62DCDB0050'.zfill(64))