Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python': 0.09; '16)': 0.09; 'encode': 0.09; 'subject:string': 0.09; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'hex': 0.16; 'integer.': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'roy': 0.16; 'subject:raw': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'string,': 0.17; 'tim': 0.18; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'wondering': 0.26; 'assert': 0.29; 'chase': 0.29; 'long.': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'you?': 0.32; 'received:84': 0.32; 'to:addr:python-list': 0.33; 'built-in': 0.35; 'subject:?': 0.35; 'something': 0.35; 'but': 0.36; 'subject:: ': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'end': 0.40; 'back': 0.62; 'header :Reply-To:1': 0.68; 'smith': 0.71; 'reply-to:no real name:2**0': 0.72; 'directly.': 0.78; '2.7.': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=H6PGvWW1HAgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=9287ROM6qSYA:10 a=5Ktul1GiloVYEIj2jo4A:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 24 Dec 2012 18:15:51 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Integer as raw hex string? References: <50D87B89.9000302@tim.thechases.com> In-Reply-To: <50D87B89.9000302@tim.thechases.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1356372951 news.xs4all.nl 6960 [2001:888:2000:d::a6]:55560 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35478 On 2012-12-24 15:58, Tim Chase wrote: > On 12/24/12 09:36, Roy Smith wrote: >> I have an integer that I want to encode as a hex string, but I don't >> want "0x" at the beginning, nor do I want "L" at the end if it happened >> to be a long. The result needs to be something I can pass to int(h, 16) >> to get back my original integer. >> >> The brute force way works: >> >> h = hex(i) >> assert h.startswith('0x') >> h = h[2:] >> if h.endswith('L'): >> h = h[:-1] >> >> but I'm wondering if there's some built-in call which gives me what I >> want directly. Python 2.7. > > Would something like > > h = "%08x" % i > > or > > h = "%x" % i > > work for you? > Or: h = "{:x}".format(i)