0

I have the following code which in Python 2.7 works exactly how I want. It takes a series of integer values in regs and converts each value to its character equivalent. For example, 21365 ==> 0x5375 amd would result in "Su".

RegString = ""
    for i in range(length):
        if regs[start+i]!=0:
            print (" Regs is ", regs[start+1], " Hex  is ", hex(regs[start+i]), " Striped is ", str(format(regs[start+i],'x') ))
            RegString = RegString + str(format(regs[start+i],'x').decode('hex'))

However in Python 3, the decode('hex') throws an error. Now, I looked at many posting with this problem, but have not been able to apply those solutons to my problem, that is to modify the above code to work in Python 3.

Here is the output when I try to run this section of code:

Regs is 20341 Hex is 0x4f75 Striped is 4f75

Traceback (most recent call last): File "v3Test.py", line 23, in Get_Regester_String RegString = RegString + str(format(regs[start+i],'x').decode('hex'))

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

Can anyone point me in the right direction to fix the above failing statement so that it will work in Python 3.7 "RegString = RegString + str(format(regs[start+i],'x').decode('hex')) ". Thanks...RDK

RDK
  • 355
  • 2
  • 7
  • 24

1 Answers1

1

For python 3 use the following process

bytes.fromhex(format(regs[start+i],'x')).decode('utf-8')
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • Many thanks. I worked on this for several hours before I posted the question. Your answer works like a champ and a lot simpler that some of the things I tried. Bravo – RDK Apr 13 '19 at 16:25