简体   繁体   中英

How do I convert this Python2 code into Python3?

I am having some trouble converting this Python 2 code:

import md5

steamid=76561197960435530
temp = ""
for i in range(8):
    temp +=  chr((steamid & 0xFF))
    steamid >>= 8
m = md5.new("BE"+temp)

print m.hexdigest()

into Python 3, the result from the code above is 'a071c58205ec529c5c19fa7dadcaae93'. However, when I try to reproduce the same code in Python 3 using the hashlib library, I get a completely different return. I want it to return the exact same thing as the Python 2 code. Here is my attempt to reproduce the code in Python 3:

from hashlib import md5

steamid = 76561197960435530
temp = ""
for i in range(8):
    temp +=  chr((steamid & 0xFF))
    steamid >>= 8
m = md5()
m.update(("BE"+temp).encode())

print(m.hexdigest())

which returns 'a96d4d4b56a3b5c1a747e01dd7045c6d' which is not what I want it to return.

In Python 3 you are building a string with the correct code points, and then using encode() to turn it into bytes. This is where the problem lies: encode() will UTF8-encode the code points in the string, which will result in a byte sequence that differs from the intended one. To avoid this problem, you should use bytes from the ground up:

from hashlib import md5

steamid = 76561197960435530
temp = bytearray()
for i in range(8):
    temp.append(steamid & 0xFF)
    steamid >>= 8
m = md5()
m.update((b"BE" + temp))

print(m.hexdigest())

As an added bonus, this version of the code also runs unchanged on Python 2.7, and produces the correct result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM