Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(using': 0.07; 'lately': 0.07; 'string': 0.09; '22,': 0.09; 'subject:keys': 0.09; 'cc:addr :python-list': 0.11; '"hello': 0.16; '16)': 0.16; 'adam': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'functions;': 0.16; 'hashlib': 0.16; 'hex': 0.16; 'integer.': 0.16; 'internally': 0.16; "languages'": 0.16; 'sha1': 0.16; 'subject:sqlite3': 0.16; 'ties': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'module,': 0.24; 'string,': 0.24; "haven't": 0.24; 'looks': 0.24; '---': 0.24; 'question': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'least': 0.26; 'header:In-Reply- To:1': 0.27; 'wondering': 0.29; 'related': 0.29; 'converting': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'getting': 0.31; "skip:' 40": 0.31; 'struct': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'pm,': 0.38; 'rather': 0.38; 'inefficient': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=/7DL406NA4LHYBRRg3PQflr/sFIFkt/9z8ihPHNSlVo=; b=dkMZ6PfNGRKoOVh7oZXzQYyU2dHSNYujUnPZC9qVF2ORX3lAtHb+l7zErubs+6LFhZ 7QRKUJzOQigVzKLqhEL5ua40YSfESbvngr4QjcLRbUi3bAlNdeYJ3UA83kkNqP8A2YKh l2QWyLkTSM5+U5oiwSK/S2OROmFdIWZ/D3SIobx00w27fHWPPsfbUK9SiJ+1dvYDnw7S pLAqQ7COeDAzI57Ed6v1epPJJwHgMw+wQfGeGOwVTWxPrCdCeGU8fM1Cl5OYef/Fdrv1 Cm0Yv9oUNY+sVkGTu9yMUTBhRVBIIbGLzO/Oj8/k7HI6eqYrMGTTUzb04XbZgyx41AkO pwSA== MIME-Version: 1.0 X-Received: by 10.58.160.134 with SMTP id xk6mr671392veb.64.1400768050574; Thu, 22 May 2014 07:14:10 -0700 (PDT) In-Reply-To: <05c15bxrpj.ln2@news.ducksburg.com> References: <05c15bxrpj.ln2@news.ducksburg.com> Date: Fri, 23 May 2014 00:14:10 +1000 Subject: Re: hashing strings to integers for sqlite3 keys From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400768058 news.xs4all.nl 2861 [2001:888:2000:d::a6]:52353 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71893 On Thu, May 22, 2014 at 11:54 PM, Adam Funk 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