inconsistency in converting from/to hex

Serhiy Storchaka storchaka at gmail.com
Sun Nov 17 11:22:50 EST 2013


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'





More information about the Python-list mailing list