You can use hash function:
>>> hash("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0")
1892010093
Question
We can convert a datetime
value in to decimal using following function.
import time
from datetime import datetime
t = datetime.now()
t1 = t.timetuple()
print time.mktime(t1)
Output :
Out[9]: 1395136322.0
Similarly is there a way to convert strings in to a decimal
using python?.
Example string.
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0"
Solution 2
You can use hash function:
>>> hash("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0")
1892010093
OTHER TIPS
If you want an integer to uniquely identify a string, I'd go for hashing functions, like SHA. They return the same value for the same input.
import hashlib
def sha256_hash_as_int(s):
return int(hashlib.sha256(s).hexdigest(), 16)
If you use Python 3, you first have to encode s
to some concrete encoding, like UTF-8.
Furthermore, take a look at the hashlib
module and decide if you really need an integer, or if the output of hexdigest()
isn't OK for you, too.