Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsreader4.netcologne.de!news.netcologne.de!xlned.com!feeder5.xlned.com!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'base64': 0.16; 'codecs': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'removed,': 0.16; '>>>': 0.22; 'import': 0.22; 'header:User- Agent:1': 0.23; 'least': 0.26; 'subject:/': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "skip:' 10": 0.31; "d'aprano": 0.31; 'steven': 0.31; 'subject:skip:i 10': 0.31; "they'll": 0.31; 'subject:from': 0.34; 'but': 0.35; 'two': 0.37; '8bit%:86': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'back': 0.62; 'received:46': 0.66; '3.4': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Serhiy Storchaka Subject: Re: inconsistency in converting from/to hex Date: Sun, 17 Nov 2013 18:22:50 +0200 References: <528862b2$0$29975$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: 46.211.126.150 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <528862b2$0$29975$c3e8da3$5496439d@news.astraweb.com> 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384705376 news.xs4all.nl 15986 [2001:888:2000:d::a6]:47803 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59756 17.11.13 08:31, Steven D'Aprano написав(ла): > There's already at least two ways to do it in Python 2: > > py> import binascii > py> binascii.hexlify('Python') > '507974686f6e' > > py> import codecs > py> codecs.encode('Python', 'hex') > '507974686f6e' Third: >>> import base64 >>> base64.b16encode(b'Python') b'507974686F6E' Fourth: >>> '%0*x' % (2*len(b'Python'), int.from_bytes(b'Python', byteorder='big')) b'507974686F6E' Fifth: >>> ''.join('%02x' % b for b in b'Python') b'507974686F6E' > [Aside: in Python 3, the codecs where (mistakenly) removed, but they'll > be added back in 3.4 or 3.5.] Only renamed. >>> import codecs >>> codecs.encode(b'Python', 'hex_codec') b'507974686f6e'