Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Adam Funk Newsgroups: comp.lang.python Subject: Re: hashing strings to integers for sqlite3 keys Date: Thu, 22 May 2014 15:47:48 +0100 Organization: $CABAL Lines: 37 Message-ID: References: <05c15bxrpj.ln2@news.ducksburg.com> X-Trace: individual.net lWz84HU0hqDc9v6YhYOKOAykBLQ5vYIJarRXrE6P766DBSefkF X-Orig-Path: news.ducksburg.com!not-for-mail Cancel-Lock: sha1:syj6tpvR8CyfywD6Eq44ouItxn0= sha1:jxO4uO+ucQGQD+O4UhrDdtee6M8= User-Agent: slrn/pre1.0.2-9 (Linux) Xref: csiph.com comp.lang.python:71897 On 2014-05-22, Chris Angelico wrote: > On Thu, May 22, 2014 at 11:54 PM, Adam Funk wrote: >> 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. Well, J*v* returns a byte array, so I used to do this: digester = MessageDigest.getInstance("MD5"); ... digester.reset(); byte[] digest = digester.digest(bytes); return new BigInteger(+1, digest); I dunno why language designers don't make it easy to get a single big number directly out of these things. I just had a look at the struct module's fearsome documentation & think it would present a good shoot(self, foot) opportunity. -- A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing on usenet and in e-mail?