Inspired by the XKCD geohashing comic (http://imgs.xkcd.com/comics/geohashing.png), I thought I'd have a go at coding a generator in Python. I've hit a block with the major part of it, though: The converting to MD5 and then to decimal.
Is it at all possible?
Answer
edit: After looking at the comic here is a more complete solution for XKCD geohashing:
>>> md5 = hashlib.md5('2005-05-26-10458.68').hexdigest() # get MD5 as hex string
>>> float.fromhex('0.' + md5[:16]) # first half as float
0.85771326770700229
>>> float.fromhex('0.' + md5[16:]) # second half as float
0.54454306955928211
Here is a more general answer for "converting to MD5 and then to decimal":
Say you want the decimal MD5 of the string 'hello world'
, you could use the following:
>>> int(hashlib.md5('hello world').hexdigest(), 16)
125893641179230474042701625388361764291L
The hash.hexdigest()
function returns a hexadecimal string, and int(hex_str, 16)
is how you can convert a hexadecimal string to a decimal.
No comments:
Post a Comment