Why does Python 3.5 Return a TypeError and not in Python 2.7
<p>I have a working piece of code in Python 2.7:</p>

<pre><code>def reversetomd5(knownhash):
  clean="" 
  for i in [1,2,3,4,5,7,8,9,10,11,13,14,15,16,18,19,20,21,22,24,25,26,27,28]:
    clean+=knownhash[i]
 b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 block=[]
 for i in xrange(2,24,3):
   p1 = b64.index(clean[i-2])
   p2 = b64.index(clean[i-1])
   p3 = b64.index(clean[i])
   block.append(p1 &lt;&lt; 12 | p2 &lt;&lt; 6 | p3)
 md5hash=""
 for i in block:
   n1 = i &gt;&gt; 8
   n2 = i &amp; 0xff
   md5hash+=chr(n1)+chr(n2)
 return binascii.hexlify(md5hash)
</code></pre>

<p>I'm trying to get it working in Python 3.5  I swapped <code>xrange</code> with <code>range</code> but the character shifting at the bottom doesn't work anymore and returns <code>TypeError: a bytes-like object is required, not 'str'</code>.  I haven't been able to figure out what changed in Python 3 to cause that.  Any help is greatly appreciated.</p>

<p>Got it working correctly.  <code>chr()</code> was replaced to return the <code>byte</code> equivalent of the character.  Had to change the <code>md5hash</code> initialization to an empty <code>byte</code> variable instead of a <code>string</code>.  Then just <code>.decode()</code> it at the end to return a nice string.</p>

<pre><code>def reversetomd5(knownhash):
  clean="" 
  for i in [1,2,3,4,5,7,8,9,10,11,13,14,15,16,18,19,20,21,22,24,25,26,27,28]:
    clean+=knownhash[i]
 b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 block=[]
 for i in range(2,24,3):
   p1 = b64.index(clean[i-2])
   p2 = b64.index(clean[i-1])
   p3 = b64.index(clean[i])
   block.append(p1 &lt;&lt; 12 | p2 &lt;&lt; 6 | p3)
 md5hash=b''
 for i in block:
   n1 = i &gt;&gt; 8
   n2 = i &amp; 0xff
   md5hash+=bytes[(n1)]+bytes[(n2)]
 return binascii.hexlify(md5hash).decode()
</code></pre>

python python-2.7 python-3.x