0

I have a file encoded in hex and I'm trying to decode the file however I keep getting a type error. I have only been using python on and off for a few weeks so if this seems like a basic question I apologize.

The file contents is as follows:

4647525137454353554e54544b5831375a42524d345742473246563639554e4a36495a3359304f35394843554637564d4d464f32354143574f495a4f4a4a565849373259544f46335a4358494b424e335047545a51534b47465259475956584d44594f473536494553373653455932574b33574431435a314d35545957594d4e57434444344948324d375858544f4c564f31444a45304947394c32375a584f4845535a534f43353859594c55594e4239363759393738313557475859345a474448434e4f5a5744544d696c6c656e69756d323030303a3035303233626566343737386639343461626439346334653364623062326166

here is the code I ran:

"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")

This is what I got back:

Traceback (most recent call last):
  File "converter.py", line 1, in <module>
    "received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")
  File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
    output = binascii.a2b_hex(input)
TypeError: Non-hexadecimal digit found
martineau
  • 119,623
  • 25
  • 170
  • 301
Rusty2012
  • 1
  • 1
  • 1
  • 1
    Have you double checked to make sure all the characters are 0-9 and A-F? I can't see any invalid characters, but there must be one. – Carcigenicate Dec 09 '16 at 16:59
  • Cannot reproduce, ```"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")``` produces ```AttributeError: 'str' object has no attribute 'decode'``` – wwii Dec 09 '16 at 17:24

1 Answers1

3

You're giving it a filename rather than the contents of that file:

"received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt".decode("hex")

Try this:

open("received_files/documents/cache/OCAGS0WFYO57JVFGUI4Z437.txt").read().decode("hex")
Will
  • 4,299
  • 5
  • 32
  • 50
  • @wwii it returns a string containing the contents of the file. – Will Dec 09 '16 at 18:41
  • ```str``` doesn't have a ```decode``` method. ```'ff'.decode("hex") --> AttributeError```. Even if it returned a ```bytes``` object, "hex" isn't a standard encoding - ```b'ff'.decode("hex") --> LookupError``` – wwii Dec 09 '16 at 19:52
  • @wwii https://docs.python.org/2/library/stdtypes.html in 2.7 it does. it's working for me, could be different in 3. Also here: http://stackoverflow.com/questions/9641440/convert-from-ascii-string-encoded-in-hex-to-plain-ascii – Will Dec 09 '16 at 20:34
  • 1
    From a comment in an answer to that SO question, it appears `str.decode()` was removed for 3. Since OP used it, I'm going to assume they're using 2. – Will Dec 09 '16 at 20:44
  • @Will no need to assume, just read this: File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode – John Machin Dec 10 '16 at 03:03