Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Use int('db931', 16) to convert the hexadecimal (base-16) string db931 to decimal.

Here is a clue - this encodes the example image and produces the numbers you will find there.

>>> from hashlib import md5
>>> hash = md5("2005-05-26-10458.68").hexdigest()
>>> hash
'db9318c2259923d08b672cb305440f97'
>>> int(hash[:16],16)/16.**16
0.8577132677070023
>>> int(hash[16:],16)/16.**16
0.5445430695592821
>>> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top