Search code examples
pythondecode

I get an error whenever I try to convert from hex to Ascii


So here is my code

import codecs
m = 61626374667b7273345f69735f61773373306d337
print(m.decode("hex"))

And this is the error that I get, I'm not sure if it is a problem within the syntax or I didn't use the library well.

AttributeError: 'str' object has no attribute 'decode'


Solution

  • This method is from python2.

    If you want to decode a string using decode from codecs you need to use:

    import codecs
    
    string = "68656c6c6f" #Your string here, between quotation mark
    binary_str = codecs.decode(string, "hex")
    print(str(binary_str,'utf-8'))
    

    This will still won't work because you provided an hex string with an ODD number of letters, and every ascii letter is represented by 2 hex digits, so recheck your string.

    (The code is from here)