hashing strings to integers for sqlite3 keys

Chris Angelico rosuav at gmail.com
Thu May 22 10:14:10 EDT 2014


On Thu, May 22, 2014 at 11:54 PM, Adam Funk <a24061 at ducksburg.com> wrote:
>>  >>> from hashlib import sha1
>>  >>> s = "Hello world"
>>  >>> h = sha1(s)
>>  >>> h.hexdigest()
>>   '7b502c3a1f48c8609ae212cdfb639dee39673f5e'
>>  >>> int(h.hexdigest(), 16)
>>   703993777145756967576188115661016000849227759454L
>
> That ties in with a related question I've been wondering about lately
> (using MD5s & SHAs for other things) --- getting a hash value (which
> is internally numeric, rather than string, right?) out as a hex string
> & then converting that to an int looks inefficient to me --- is there
> any better way to get an int?  (I haven't seen any other way in the
> API.)

I don't know that there is, at least not with hashlib. You might be
able to use digest() followed by the struct module, but it's no less
convoluted. It's the same in several other languages' hashing
functions; the result is a string, not an integer.

ChrisA



More information about the Python-list mailing list