Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59756

Re: inconsistency in converting from/to hex

From Serhiy Storchaka <storchaka@gmail.com>
Subject Re: inconsistency in converting from/to hex
Date 2013-11-17 18:22 +0200
References <mailman.2742.1384640221.18130.python-list@python.org> <528862b2$0$29975$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.2786.1384705376.18130.python-list@python.org> (permalink)

Show all headers | View raw


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'

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

inconsistency in converting from/to hex Laszlo Nagy <gandalf@shopzeus.com> - 2013-11-16 23:16 +0100
  Re: inconsistency in converting from/to hex Ned Batchelder <ned@nedbatchelder.com> - 2013-11-16 15:35 -0800
  Re: inconsistency in converting from/to hex Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-17 06:31 +0000
    Re: inconsistency in converting from/to hex Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-17 12:38 +0000
    Re: inconsistency in converting from/to hex Serhiy Storchaka <storchaka@gmail.com> - 2013-11-17 18:22 +0200

csiph-web