Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: binascii.a2b_hex gives "Odd-length string"

Tags:

python

I have a hex value that I'm grabbing from a text file, then I'm passing it to a2b_hex to convert it to the proper binary representation. Here is what I have:

k = open('./' + basefile + '.key', 'r')
k1 = k.read()
k.close()
my_key = binascii.a2b_hex(k1)

When I print k1, it is as expected: 81e3d6df

Here is the error message:

Traceback (most recent call last):
  File "xor.py", line 26, in <module>
    my_key = binascii.a2b_hex(k1)
TypeError: Odd-length string

Any suggestions? Thanks!

like image 331
Magicked Avatar asked Sep 16 '10 22:09

Magicked


1 Answers

I suspect there is a trailing newline at the end of the file. Strip the string before passing it to binascii.

Note there's now also a simpler spelling: k1.strip().decode('hex').

like image 70
bobince Avatar answered Sep 22 '22 13:09

bobince